A sorter class to sort an array based on other arrays.
//+------------------------------------------------------------------+
//| class MultiSort<TItem,TKey1,TKey2,TKey3>. |
//| Usage: Sorter class to sort an array based on other arrays. |
//+------------------------------------------------------------------+
template<typename TItem,typename TKey1,typename TKey2,typename TKey3>
class MultiSort
{
public:
//--- method to sort items by keys
void SortBy(TItem& items[],
TKey1& keys1[],
TKey2& keys2[],
TKey3& keys3[],
bool ascending_order1=true,
bool ascending_order2=true,
bool ascending_order3=true);
};
Note:
Empty keys arrays are ignored by the sorter.
For example to sort position tickets by position open time (as the only sort key):
//--- sort tickets by position open time only
MultiSort<long,datetime,int,int> sorter;
int empty[];
sorter.SortBy(tickets,times,empty,empty);
Here is a complete example
//+------------------------------------------------------------------+
//| MultiSort_demo.mq5 |
//| Copyright © 2018, Amr Ali |
//| https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2018, Amr Ali"
#property description "Multi sort example - Sort position tickets by symbol then by open time then by open price in accending or descending order."
#property version "1.000"
#property script_show_inputs
#include <MultiSort.mqh>
//--- input variables
input bool mode_asc_symbols = true; // ascending order for symbols
input bool mode_asc_times = true; // ascending order for time
input bool mode_asc_prices = true; // ascending order for price
//--- global variables
long tickets[]= {88806796,90462225,91039722,91200504,90389608,88429143,89452623,91487721,89323610,90439077,89355691,88943822};
string symbols[]= {"USDCAD","EURCHF","GBPCHF","EURUSD","EURUSD","EURCHF","AUDUSD","EURUSD","EURUSD","GBPCHF","EURCHF","AUDUSD"};
datetime times[]= {D'2020.12.07 09:31:40',D'2020.12.10 09:55:30',D'2020.12.07 03:16:05',D'2020.12.07 03:45:39',D'2020.12.09 14:34:42',
D'2020.12.08 14:00:13',D'2020.12.07 12:34:42',D'2020.12.08 17:37:28',D'2020.12.10 02:34:55',D'2020.12.09 12:00:09',
D'2020.12.10 17:56:09',D'2020.12.07 08:30:14'};
double prices[]= {1.24428,1.35444,1.37244,1.28973,1.39795,1.39960,1.15292,1.19290,1.11015,1.17936,1.37226,1.34722};
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
Print("-------before sort-------");
for(int i=0; i<ArraySize(tickets); i++)
PrintFormat("%8i | %8s | %8s | %8.5f",tickets[i],symbols[i],(string)times[i],prices[i]);
//--- sort position tickets by symbol, open time and open price.
MultiSort<long, string, datetime, double> multi_sorter;
multi_sorter.SortBy(tickets,
symbols,
times,
prices,
mode_asc_symbols,
mode_asc_times,
mode_asc_prices);
Print("-------MultiSort.SortBy(tickets,symbols,times,prices)-------");
for(int i=0; i<ArraySize(tickets); i++)
PrintFormat("%8i | %8s | %8s | %8.5f",tickets[i],symbols[i],(string)times[i],prices[i]);
}
//+------------------------------------------------------------------+
/* Sample output
-------before sort-------
88806796 | USDCAD | 2020.12.07 09:31:40 | 1.24428
90462225 | EURCHF | 2020.12.10 09:55:30 | 1.35444
91039722 | GBPCHF | 2020.12.07 03:16:05 | 1.37244
91200504 | EURUSD | 2020.12.07 03:45:39 | 1.28973
90389608 | EURUSD | 2020.12.09 14:34:42 | 1.39795
88429143 | EURCHF | 2020.12.08 14:00:13 | 1.39960
89452623 | AUDUSD | 2020.12.07 12:34:42 | 1.15292
91487721 | EURUSD | 2020.12.08 17:37:28 | 1.19290
89323610 | EURUSD | 2020.12.10 02:34:55 | 1.11015
90439077 | GBPCHF | 2020.12.09 12:00:09 | 1.17936
89355691 | EURCHF | 2020.12.10 17:56:09 | 1.37226
88943822 | AUDUSD | 2020.12.07 08:30:14 | 1.34722
-------MultiSort.SortBy(tickets,symbols,times,prices)-------
88943822 | AUDUSD | 2020.12.07 08:30:14 | 1.34722
89452623 | AUDUSD | 2020.12.07 12:34:42 | 1.15292
88429143 | EURCHF | 2020.12.08 14:00:13 | 1.39960
90462225 | EURCHF | 2020.12.10 09:55:30 | 1.35444
89355691 | EURCHF | 2020.12.10 17:56:09 | 1.37226
91200504 | EURUSD | 2020.12.07 03:45:39 | 1.28973
91487721 | EURUSD | 2020.12.08 17:37:28 | 1.19290
90389608 | EURUSD | 2020.12.09 14:34:42 | 1.39795
89323610 | EURUSD | 2020.12.10 02:34:55 | 1.11015
91039722 | GBPCHF | 2020.12.07 03:16:05 | 1.37244
90439077 | GBPCHF | 2020.12.09 12:00:09 | 1.17936
88806796 | USDCAD | 2020.12.07 09:31:40 | 1.24428
*/