Introsort_pfn/sort_simple_array.mq5
amrali a832e062c0
2026-07-25 01:57:19 +03:00

28 lines
1.3 KiB
MQL5

//+------------------------------------------------------------------+
//| sort_simple_array.mq5 |
//| Copyright © 2025, Amr Ali |
//| https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#include "Introsort.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnStart()
{
// Sorting string array in ascending order (default, no compare function)
string stringArray[] = {"cherry", "apple", "date", "banana"};
Introsort(stringArray);
ArrayPrint(stringArray);
// For sorting string array in descending order,
// We define a pointer type to custom Less function.
typedef bool (*pDescending)(string &left, string &right);
Introsort(stringArray, (pDescending)Descending);
ArrayPrint(stringArray);
}
//+------------------------------------------------------------------+
// Custom Less function to sort in descending order.
bool Descending(string &left, string &right)
{ return left > right; }