Features of the mql5 language, subtleties and tricks - page 47
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Like this one?
https://www.mql5.com/ru/code/9336
The question was about an array of structures and sorting by structure fields.
Has anyone done resource-efficient sorting of an array of structures by any (not string) given field of the structure?
Suppose there is a structure with three fields int, datetime and double, and there is an array filled with data consisting of this structure. In each cell of the array, the fields of the structure are populated.
How to sort this array by any of these fields?
Create array double[][2], fill it as { fieldvalue, arrayindex }. Sort by regular ArraySort (by first change). And then you sort structures in array according to indexes.
I wanted to bypass this method. I thought, maybe there are other ways.
Why bypass it? You can't think of anything faster, because the sorting is done by a native function.
Why bypass it? You can't think of anything faster, because the sorting is done by a native function.
Has anyone done resource-efficient sorting of an array of structures by any (not string) given field of the structure?
Suppose there is a structure with three fields int, datetime and double, and there is an array filled with data consisting of this structure. In each cell of the array, the fields of the structure are populated.
How to sort this array by any of these fields?
Hi, if you need some kind of universal sorting function that can sort an array of complex data type, that's impossible in principle.
If you need capability of multifactor sorting of some known type, you can do it using classes, and CArrayObj in particular is designed for it.
I should add that even in languages as mature as C# the sorting of complex objects is solved via custom IComparer. So you will have to write a sorting criterion yourself anyway.
I wanted to bypass this method. I thought, maybe there are other ways.
There are two ways.
The number one way is to set the operator <
Way number two functionaries.
Method number one:
Method number two is similar, only the operator is completely external and is passed to sorting. It's a bit more complicated, but much more versatile.
If you need, I can give you some ideas, but only later.
Sorting just copypaste some kodobase, if you need faster, you'll have to write your own one, but you'll write it once and never worry about it again.Method number two is similar, only the operator is completely external and is passed to sorting. It is a bit more complicated but much more versatile.
That is, you have to use pointers to functions