Introsort_pfn/sort_string_array.mq5

30 lines
2.5 KiB
MQL5
Raw Permalink Normal View History

2025-09-19 20:20:45 +00:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| sort_string_array.mq5 |
//| Copyright <EFBFBD> 2018, Amr Ali |
//| https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#include "Introsort.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnStart()
{
// Sorting string array (ascending order - default)
string stringArray[] = {"cherry", "apple", "date", "banana"};
Introsort(stringArray);
ArrayPrint(stringArray);
// Define a pointer type to custom Less function.
typedef bool (*pDescending)(string left, string right);
// Sorting string array (descending order)
Introsort(stringArray, (pDescending)Descending);
ArrayPrint(stringArray);
}
//+------------------------------------------------------------------+
// Custom Less function to sort in descending order.
bool Descending(string left, string right)
{ return left > right; }