d.bignotti: If i need to sort out a struct like based on Symbolname? is this possible using standard library?
struct MyStruct{ ... }; MyStruct Str[]; |
|
Hello,
is there a way to sort a struct based on an element inside it?
If i need to sort out a struct like based on Symbolname? is this possible using standard library? I'm pretty new to programming and i can't understand everything, but i readed that CArrayObj of MQL 5 Reference
"Class CArrayObj provides the ability to work with a dynamic array of pointers to instances of CObject and its derived classes. This allows working both with multidimensional dynamic arrays of primitive data types and with data structures that have more complex organization of data."
I would like to learn to use standard library as much as i can, anyone could please help me with this example?
Thanks
EDIT: Can someone suggest me some reading to understand OOP/Pointers and more Adavanced stuff in MQL 5/C++? I'm learning to programm through example basically. I can't understand OOP in general with all this pointer stuff.
//+------------------------------------------------------------------+ //| sorting objects.mq5 | //| nicholishen | //| www.reddit.com/u/nicholishenFX | //+------------------------------------------------------------------+ #property copyright "nicholishen" #property link "www.reddit.com/u/nicholishenFX" #property version "1.00" #define SORT_ASCENDING 1 #define SORT_DESCENDING 2 //I made this its own include file and include in my projects. You could do the same. #include <Arrays\ArrayObj.mqh> template<typename T> class objvector : public CArrayObj { public: T *operator[](const int index) const { return (T*)At(index);} }; //........... class MyClass : public CObject { public: double _High[]; double _Low[]; string Symbolname; MyClass(string name):Symbolname(name){} virtual int Compare(const CObject *node,const int mode=0) const override; }; int MyClass::Compare(const CObject *node,const int mode=0)const { MyClass *that = (MyClass*)node; int comp = StringCompare(this.Symbolname,that.Symbolname); if(mode == SORT_DESCENDING) return -comp; else return comp; } //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- objvector<MyClass> my; my.Add(new MyClass("USDJPY")); my.Add(new MyClass("CADJPY")); my.Add(new MyClass("GBPJPY")); my.Add(new MyClass("EURUSD")); my.Sort(SORT_DESCENDING); for(int i=0;i<my.Total();i++) { Print(my[i].Symbolname); } Print("---------"); my.Sort(SORT_ASCENDING); for(int i=0;i<my.Total();i++) { Print(my[i].Symbolname); } } //+------------------------------------------------------------------+
|
I'll try to go with option n°1. Something like nicholishen already did probably, thank you very much. Difficult part now is to understand why it works

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
is there a way to sort a struct based on an element inside it?
If i need to sort out a struct like based on Symbolname? is this possible using standard library? I'm pretty new to programming and i can't understand everything, but i readed that CArrayObj of MQL 5 Reference
"Class CArrayObj provides the ability to work with a dynamic array of pointers to instances of CObject and its derived classes. This allows working both with multidimensional dynamic arrays of primitive data types and with data structures that have more complex organization of data."
I would like to learn to use standard library as much as i can, anyone could please help me with this example?
Thanks
EDIT: Can someone suggest me some reading to understand OOP/Pointers and more Adavanced stuff in MQL 5/C++? I'm learning to programm through example basically. I can't understand OOP in general with all this pointer stuff.