derivado de amrali/Introsort
60 linhas
1,9 KiB
MQL5
//+------------------------------------------------------------------+
| |||
//| sort_struct_array_custom.mq5 |
| |||
//| Copyright © 2025, 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);
| |||
}
| |||
//+------------------------------------------------------------------+
|