Need some help implementing an array sort in MQL5

 

I've just started playing around with MT5 and I'm loving the additional functionality in the strategy tester - in fact, I'm building some EAs that I've been putting off due to the inability to test them under MT4.

Anyway, I've hit a bump in the road and I'm hoping someone can show me how to achieve the equivalent of the following MQL4 code:

 

double Results[][2];

ArrayResize(Results, 0);

for (int Index = 0; Index < 5; Index++)

{

    if (Trades[Index][0] > 0)

    { 

        ArrayResize(Results, ArrayRange(Results, 0));

        Results[ArrayRange(Results, 0) - 1][0] = Trades[Index][0];

        Results[ArrayRange(Results, 0) - 1][1] = Index;

    } 

if (ArrayRange(Results, 0) > 0)

    ArraySort(Results, WHOLE_ARRAY, 0, MODE_ASCEND);

 

The solution can be implemented within the OnTick() function or as a separate function so whichever way is easiest is fine with me.

I'd also appreciate a note explaining how to sort the array in descending order.

 
fx-raider:

I've just started playing around with MT5 and I'm loving the additional functionality in the strategy tester - in fact, I'm building some EAs that I've been putting off due to the inability to test them under MT4.

Anyway, I've hit a bump in the road and I'm hoping someone can show me how to achieve the equivalent of the following MQL4 code:

 

double Results[][2];

ArrayResize(Results, 0);

for (int Index = 0; Index < 5; Index++)

{

    if (Trades[Index][0] > 0)

    { 

        ArrayResize(Results, ArrayRange(Results, 0));

        Results[ArrayRange(Results, 0) - 1][0] = Trades[Index][0];

        Results[ArrayRange(Results, 0) - 1][1] = Index;

    } 

if (ArrayRange(Results, 0) > 0)

    ArraySort(Results, WHOLE_ARRAY, 0, MODE_ASCEND);

 

The solution can be implemented within the OnTick() function or as a separate function so whichever way is easiest is fine with me.

I'd also appreciate a note explaining how to sort the array in descending order.

1. Post the codes using SRC button, it will make easier for others who's trying to help you, to read your codes. If the code is long enough, attach it.

 

2. You only show partial of mql4 code, which is actually not helping at all. If you insist, that mql4 code is pretty much the same when converted to mql5.

3. Here's Array Functions in mql5. 

 
phi.nuts:

1. Post the codes using SRC button, it will make easier for others who's trying to help you, to read your codes. If the code is long enough, attach it.

 

2. You only show partial of mql4 code, which is actually not helping at all. If you insist, that mql4 code is pretty much the same when converted to mql5.

3. Here's Array Functions in mql5. 

Hi, phi.nuts

1) I didn't know about the SRC button - thanks

2) I think I actually show a pretty complete MQL4 example ... beyond needing the Trades array defined & populated, that snippet can basically be pasted into an MQL4 script and will run

3) I already looked at that Array Functions reference but, in MQL5, ArraySort is only capable of sorting a single-dimension array in ascending order, which makes it totally different to the MQL4 ArraySort function - pretty stupid really, when the other array functions seem to work fairly interchangably.

As far as I could work out, replicating the ArraySort(Results, WHOLE_ARRAY, 0, MODE_ASCEND); line involved "including" an array module, creating a structure, instantiating an array of the structure, using completely different commands to expand the array & add records, attempting to sort the array instance, then having to dispose of it after use.

After an hour of pulling my hair out, I just gave up and coded a simple bubble sort which did the job fine as the array was only 20 records in size anyway.

Reason: