By the way, if someone is interested in programming dates, we could write a mini-library for working with dates before 1970. There was some life there)).
For programming tasks not related to market/forex.
On Windows, the FILETIME type stores time as a count of 100-nanosecond intervals that have elapsed since 0:00 GMT on 1 January 1601. But, then you have to use Windows API functions, instead and then it is also not possible to import these old dates intocustom symbols.
to get a Sort template ( array_pointers, compare_function )
While using a sort function that accepts a function pointer to compare function is the standard approach for sorting array structures or object pointers, there is a much simpler technique "Operator overloading" that works with a plain sort function without the need for writing a separate compare function.
Sorting array of structures by operator overloading:
//+------------------------------------------------------------------+//| Templated class |//+------------------------------------------------------------------+template <typename T>
class Foo
{
public:
Foo(void) { payload=(T)10*MathRand()/MathRand(); }
~Foo(void) { }
public:
T payload;
public:
//--- operator overloading for sortingbooloperator < (Foo<T> &other) { returnthis.payload < other.payload; }
};
//--------------------------------------------------------------------------------------------// TEMPLATE SORTING// function is templated for any type of array (e.g., int[], double[], pointers to objects[]).//--------------------------------------------------------------------------------------------template <typename T>
void Sort(T &a[])
{
for(int i=1; i < ArraySize(a); ++i)
for(int j=i; j>0 && (a[j] < a[j-1]); j--)
{
T tmp=a[j]; a[j]=a[j-1]; a[j-1]=tmp;
}
}
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+voidOnStart()
{
Foo<int> *ptrArray[7]; // array of object pointersfor(int i=0;i<7;i++) {
ptrArray[i]=new Foo<int>();
}
Sort(ptrArray);
for(int i=0;i<7;i++) {
printf("%d. payload=%s",i,(string)ptrArray[i].payload);
delete ptrArray[i];
}
}
Also for sorting array of structures, we add an overloaded operator:
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+struct MyStruct
{
int A;
int B;
int C;
//--- operator overloading for sortingbooloperator < (MyStruct &other) { returnthis.A < other.A; }
};
This allows to simply sort like: Sort(structArray);
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
By the way, if someone is interested in programming dates, we could write a mini-library for working with dates before 1970. There was some life there)).
For programming tasks not related to market/forex.
Who knows how to make this work ?
to get a Sort template ( array_pointers, compare_function )
While using a sort function that accepts a function pointer to compare function is the standard approach for sorting array structures or object pointers, there is a much simpler technique "Operator overloading" that works with a plain sort function without the need for writing a separate compare function.
Sorting array of structures by operator overloading:
Also for sorting array of structures, we add an overloaded operator:
This allows to simply sort like: Sort(structArray);
We also add an overloaded operator to sort arrays of structures:
This allows simple sorting like: Sort(structArray);
It is often necessary to sort such an array by different fields.
There seem to be many implementations of such sorting in this thread. When the user has almost nothing to write.
It is often necessary to sort such an array by different fields.
I think there were many implementations of such sorting in this thread. When the user doesn't need to write almost anything.
user-user:
INC_SORT_BY(array,field) - sort an array of structures by a given field;
Shorter is hardly possible anymore :-)
Forum on trading, automated trading systems and testing trading strategies
Features of mql5 language, subtleties and techniques of work
Maxim Kuznetsov, 2025.03.21 09:30 AM
Forum on trading, automated trading systems and testing trading strategies
Features of mql5 language, subtleties and techniques of work
Maxim Kuznetsov, 2025.03.21 09:30
Forum on trading, automated trading systems and testing trading strategies
Features of mql5 language, subtleties and techniques of work
Maxim Kuznetsov, 2025.03.21 09:30 AM
it's all handwritten :-)
my computer died at the weekend, now I'm doing it all over again and I haven't managed to get the sources from the backups yet
in the given code except that names do not like...a little differently it is necessary to name.
PS/ there are still problems with Print() and templates/arrays in MQL. The built-in Print does not work like all the others
PPS/ ArrayPrint is inconvenient because it prints the whole array (they can be of epic size). You still have to redefine it, reducing the output