Introsort/sort_structure_array.mq5

61 lines
3.9 KiB
MQL5
Raw Permalink Normal View History

2025-09-19 20:20:20 +00:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| sort_structure_array.mq5 |
//| Copyright <EFBFBD> 2018, Amr Ali |
//| https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#include "Introsort.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
struct CRecord
{
int key1;
int key2;
int key3;
};
//+------------------------------------------------------------------+
//| a custom comparison function Less. |
//+------------------------------------------------------------------+
bool Less(const CRecord& x, const CRecord& y)
{
if(x.key1 < y.key1) return(true);
if(x.key1 > y.key1) return(false);
if(x.key2 < y.key2) return(true);
if(x.key2 > y.key2) return(false);
if(x.key3 < y.key3) return(true);
if(x.key3 > y.key3) return(false);
//--- all keys are equal
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnStart()
{
CRecord array[25];
//--- Generate random data.
for(int i = 0; i < 25; i++)
{
array[i].key1 = rand() % 10;
array[i].key2 = rand() % 10;
array[i].key3 = rand() % 10;
}
Print("Before sort:");
ArrayPrint(array);
//--- Sort the input array in-place using comparison function less.
Introsort(array);
Print("After sort:");
ArrayPrint(array);
}
//+------------------------------------------------------------------+