Array of Struct Sort using standard LIbrary

 

Hello,

is there a way to sort a struct based on an element inside it? 


   struct MyStruct
     {
      double            _High[];
      double            _Low[];
      string            Symbolname;
     };

   MyStruct Str[];

   int Size=10;

   ArrayResize(Str,Size);

   for(int i=0;i<Size;i++)
     {
      Str[i].Symbolname=SymbolName(i,false);
      Print(Str[i].Symbolname);
     }

  //How to sort now based on Symbol Name?


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.
 

 
d.bignotti: If i need to sort out a struct like based on Symbolname? is this possible using standard library?
struct MyStruct{ ... };
MyStruct Str[];

  1. Not possible. The library requires pointers to objects derived from CObject with virtual method Compare overridden. You can not use an array, must use cArray. Structures can be derived from structures only and do not have virtual functions. If you use a class, then write your compare function and use cArray you are good to go.
  2. An alternate is write a struct with a assignment operator and operator<, use regular arrays instead of pointers, and use my insertion sort Sort multiple arrays - MQL4 and MetaTrader 4 - MQL4 programming forum
 
d.bignotti:

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);
   }
}
//+------------------------------------------------------------------+
 
whroeder1:
  1. Not possible. The library requires pointers to objects derived from CObject with virtual method Compare overridden. You can not use an array, must use cArray. Structures can be derived from structures only and do not have virtual functions. If you use a class, then write your compare function and use cArray you are good to go.
  2. An alternate is write a struct with a assignment operator and operator<, use regular arrays instead of pointers, and use my insertion sort Sort multiple arrays - MQL4 and MetaTrader 4 - MQL4 programming forum


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

Reason: