Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

MultiSort - Sorting Algorithm - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 5 reads | 0 comments |

A sorter class for sorting arrays based on other arrays.

 //+------------------------------------------------------------------+
//| class MultiSort . |
//|Usage: The Sorter class sorts an array based on other arrays.      |
//+------------------------------------------------------------------+
Template < type name T item, type name TKey1, type name TKey2, type name T key 3> class multi-sort { People :
//--- Method for sorting items by key is blank SortBy(TItem& items[],
                            TKey1&keys1[],
                            TKey2&keys2[],
                            TKey3&keys3[],
boolean ascending1= true ,
boolean ascending2= true ,
boolean ascending 3 = true );
  };


The sorter ignores empty key arrays.

For example, to sort warehouse receipts by holding time (as the only sort key):

 //--- Sort tickets by holding time only Multi-sort< long , datetime , integer , integer > sorter;
integer empty[];

  sorter.SortBy(number of votes, times, empty, empty);


Here is a complete example

//+------------------------------------------------------------------+
//| MultiSort_demo.mq5 |
//| Copyright © 2018, Eminem Ali |
//| https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#Property Copyright "Copyright © 2018, Eminem Ali"
# Property description "Multiple sorting example - sort warehouse receipts by symbol, then by opening time, then by opening price in ascending or descending order."
# Property version "1.000"
#Property script display input

#Include <MultiSort.mqh> //--- Input variables
input boolean mode_asc_symbol= true ; // ascending symbol order
Input boolean mode_asc_times = true ; // Ascending time order
Input boolean mode_asc_prices = true ; // Prices in ascending order

//--- global variables
Long ticket[] = { 88806796 , 90462225 , 91039722 , 91200504 , 90389608 , 88429143 , 89452623 , 91487721 , 89323610 , 90439077 ; " Australian dollar against US dollar" } ; Date time 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 price[]= { 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 startup function |
//+------------------------------------------------------------------+
blank start ()
  {
Print ( "--------before sorting-----" );
for ( integer i = 0 ; i < array_size (tickets); i++)
Print format ( "%8i | %8s | %8s | %8.5f" , ticket[i], symbol[i], ( thin string ) time[i], price[i]); //--- Sort warehouse receipts by transaction type, opening time and opening price.  multisort< long ,ass="keyword">string, datetime , double >multi-sorter; multi_sorter.SortBy(tickets, symbols, times, price, mode_asc_symbols, mode ascending times, mode_asc_prices); print ( "--------MultiSort.SortBy(tickets, symbols, times, prices)-----" ); for ( integer i = 0 ; i < array_size (tickets); i++) print format ( "%8i | %8s | %8s | %8.5f" ,ticket[i],symbol[i],( thin string )time[i],price[i]); } //+------------------------------------------------------------------+ /* Sample output --------Before sorting-------- 88806796 | USD to CAD | 2020.12.07 09:31:40 | 1.24428 90462225 | EURUSD | 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 | EUROCHF | 2020.12.08 14:00:13 | 1.39960 89452623 | AUD/USD | 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 | EUR/CHF | 2020.12.10 17:56:09 | 1.37226 88943822 | AUD/USD | 2020.12.07 08:30:14 | 1.34722 -------MultiSort.SortBy(ticket, symbol, time, price)-------- 88943822 | AUD/USD | 2020.12.07 08:30:14 | 1.34722 89452623 | AUD/USD | 2020.12.07 12:34:42 | 1.15292 88429143 | EUR/CHF | 2020.12.08 14:00:13 | 1.39960 90462225 | EUR/CHF | 2020.12.10 09:55:30 | 1.35444 89355691 | EURUSD | 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 | USD/CAD | 2020.12.07 09:31:40 | 1.24428 */



Attachment download

📎 multisort_demo.mq5 (8.17 KB)

📎 multisort.mqh (15.02 KB)

Source: MQL5 #32665

Verification code Refresh