Access struct element using its string value - page 2

 
eatrader1231231231231233r5235134 # :

@Carl Schreiber: I came across this behaviour because of another issue I tried to solve. I have a struct which conatins several information such as open price , different stop losses, open times, closing times etc. I wanted to find the lowest and second lowest stop loss within the struct. Since ArraySort is not applicable to structs I tried the following:

....

Try this to sort your structure.


 #define ArraySortStruct(T, ARRAY, FIELD)                                         \
{                                                                                \
   class SORT                                                                     \
  {                                                                              \
   private :                                                                       \
     static void Swap( T &Array[], const int i, const int j )                     \
    {                                                                            \
       const T Temp = Array[i];                                                   \
                                                                                 \
      Array[i] = Array[j];                                                       \
      Array[j] = Temp;                                                           \
                                                                                 \
       return ;                                                                    \
    }                                                                            \
                                                                                 \
     static int Partition( T &Array[], const int Start, const int End )           \
    {                                                                            \
       int Marker = Start;                                                        \
                                                                                 \
       for ( int i = Start; i <= End; i++)                                         \
         if (Array[i]. ##FIELD <= Array[End]. ##FIELD)                               \
        {                                                                        \
          SORT::Swap(Array, i, Marker);                                          \
                                                                                 \
          Marker++;                                                              \
        }                                                                        \
                                                                                 \
       return (Marker - 1 );                                                       \
    }                                                                            \
                                                                                 \
     static void QuickSort( T &Array[], const int Start, const int End )          \
    {                                                                            \
       if (Start < End)                                                           \
      {                                                                          \
         const int Pivot = Partition(Array, Start, End);                          \
                                                                                 \
        SORT::QuickSort(Array, Start, Pivot - 1 );                                \
        SORT::QuickSort(Array, Pivot + 1 , End);                                  \
      }                                                                          \
                                                                                 \
       return ;                                                                    \
    }                                                                            \
                                                                                 \
   public :                                                                        \
     static void Sort( T &Array[], int Count = WHOLE_ARRAY , const int Start = 0 ) \
    {                                                                            \
       if (Count == WHOLE_ARRAY )                                                  \
        Count = :: ArraySize (Array);                                              \
                                                                                 \
      SORT::QuickSort(Array, Start, Start + Count - 1 );                          \
                                                                                 \
       return ;                                                                    \
    }                                                                            \
  };                                                                             \
                                                                                 \
  SORT::Sort(ARRAY);                                                             \
}

 void OnStart ()
{
   MqlRates Rates[];
  
   CopyRates ( _Symbol , PERIOD_CURRENT , 0 , 5 , Rates);
  
   ArrayPrint (Rates);
  
  ArraySortStruct( MqlRates , Rates, open);
   ArrayPrint (Rates);

   //ArraySortStruct(MqlRates, Rates, high); 
   //ArrayPrint(Rates); 

   //ArraySortStruct(MqlRates, Rates, time); 
   //ArrayPrint(Rates);