Learning logic - page 15

 
hrenfx:

Sometimes you need to sort the data in MQL4 without touching the data itself. There are no pointers in MQL4, there are no structures either.

Alternative variant is to create array Positions[][2], copy the parameter to be sorted by the zero index and give the first index to the positions. Then sort the array with standard function AgraSort.

The advantage is that AgraSort uses an efficient sorting algorithm, moreover, it works in native code.

 
Candid:

Alternatively, you can create an array Positions[][2], copy the parameter to be sorted by the zero index and give the first index to the positions. Then sort the array with standard function AgraSort.

Could you convert my script, on the previous page, using your method? I don't understand the description.
 
hrenfx:
Could you convert my script, on the previous page, using your method? I don't understand the description.

I liked the way suggested by https://www.mql5.com/ru/users/candid. Used to make it a bit more complicated. I will use a simpler one. You can sort by any attribute. Very convenient.
 
hrenfx:
Could you convert my script, on the previous page, using your method? I don't understand the description.

I won't give you the whole script, but I will give you a sketch of the function, it should be enough to understand the idea

void SortArrayINT( int& Array[], int& Positions[][]) {
  int i, Size = ArraySize(Array);
    
  ArrayResize(Positions, Size);
  
  for (i = 0; i < Size; i++) {
    Positions[i][0] = Array[i];
    Positions[i][1] = i;
  }
  ArraySort(Positions);
  return;
}


Now, Positions[0][1] contains the index of the smallest Array element, Positions[1][1] contains the index of the next largest Array element and Positions[i][1] contains the index of the i-th element.

 
Candid:

I won't write the whole script, but I will give you a sketch of the function, it should be enough to understand the idea.

I did not know about the peculiarities of working with multidimensional arrays in MQL4. Thank you, the method works:

void SortArrayINTCandid( int Array[], int& Positions[][] )
{
  int Size = ArraySize(Array);
    
  ArrayResize(Positions, Size);
  
  for (int i = 0; i < Size; i++)
  {
    Positions[i][0] = Array[i];
    Positions[i][1] = i;
  }

  ArraySort(Positions);

  return;
}

void PrintInfo( string SortName, int Array[] )
{
  int PositionsCandid[][2];
  
  SortArrayINTCandid(Array, PositionsCandid);
  
  Print("Sort by " + SortName);
  Print("SYMBOL   PROFIT   POS   SL");

  for (int i = 0; i < AMOUNT; i++)
    Print(Symbols[PositionsCandid[i][1]] + "   " + Profit[PositionsCandid[i][1]] + "   " +
          Pos[PositionsCandid[i][1]] + "   " + SL[PositionsCandid[i][1]]);
          
  Print("");
          
  return;
}
Reason: