MQL4 Reference Array Functions ArraySort

ArraySort

Sorts numeric arrays by first dimension. The AS_SERIES flag is taken into account in sorting.

bool  ArraySort(
   void&       array[],                // array for sorting
   int         count=WHOLE_ARRAY,      // count
   int         start=0,                // starting index
   int         direction=MODE_ASCEND   // sort direction
   );

Parameters

array[]

[in][out]  Numeric array for sorting.

count=WHOLE_ARRAY

[in]  Count of elements to sort. By default, it sorts the whole array.

start=0

[in]  Starting index to sort. By default, the sort starts at the first element.

direction=MODE_ASCEND

[in]  Sort direction. It can be any of the following values:

MODE_ASCEND sort in ascend direction,
MODE_DESCEND sort in descend direction.

Return Value

The function returns true on success, otherwise  - false.

Example:

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- example of sorting of one dimensional array
   double num_array[5]={4,1,6,3,9};
//--- now array contains values 4,1,6,3,9
   ArraySort(num_array);
//--- now array is sorted 1,3,4,6,9
   ArraySort(num_array,WHOLE_ARRAY,0,MODE_DESCEND);
//--- now array is sorted 9,6,4,3,1
 
//--- example of sorting of two dimensional array
   int  DataArray[5][2]={{7,3},{3,1},{57,14},{12,4},{11,1}};
//--- sorting of DataArray[][] by first dimension  (ascending)
   ArraySort(DataArray,WHOLE_ARRAY,0,MODE_ASCEND);
//--- print sorted array
   for(int i=0; i<5; i++)
     {
      string str="index "+IntegerToString(i)+": ";
      for(int j=0; j<2; j++)
        {
         if(j==0) str+="{"else str+=",";
         str+=IntegerToString(DataArray[i,j]);
         if(j==1) str+="}";
        }
      Print(str);
     }
//--- output
//index 0: {3,1}
//index 1: {7,3}
//index 2: {11,1}
//index 0: {12,4}
//index 4: {57,14}
  }