Array of Pointers

 

Hi,

I have an array of Pointers like  

Order* m_orders[]

and there is a problem:

 Order is a class containg informations about TP, SL, open time and so on.

I want to sort this array by open time in a function but mql(4) accepts only arrays by reference as parameter so I can't do that.

In C language it is quite simple:

void Sort(Order *pointer, int size)
{
    Order *i, *j, temp;

    for(i = pointer; i < pointer + size; i++)
   {
        for(j = i + 1; j < pointer + size; j++)
        {
            if((*j).OrderDate < (*i).OrderDate)
            {
                temp = *j;
                *j = *i;
                *i = temp;
            }
        }
    }
}

 

 Any tips ?

Thank you 

 

What about using struct and an array of structs?

struct _Orders {
    datetime OrderDate;
    double SL,TG,..;
    int Ticket;
    ...
};
_Orders ArrOrders[];
...
ArrayResize(ArrOrders,5);
...
void sort(_Orders& arr) {
    ...
    if (arr[x].OrderDate < arr[y].OrderDate ) { ... }
}
// NOT TESTED!!
 
MarcoNeri: I want to sort this array by open time in a function but mql(4) accepts only arrays by reference as parameter so I can't do that.
In C language it is quite simple:
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Why not?
  3. It's just as simple in mt4. Even easier and much more efficient if you use an insertion sort. (Never use bubble sort - ever.)
    Not tested
    class Order{
     public: 
       datetime OrderDate(void){return 0;}
    };
    //////////////////////////////////////
    void Sort(Order* &Array[], int size){
       for(int i=1; i < size; ++i){
          Order*   temp = Array[i];
          datetime od   = temp.OrderDate();
          while(i>0 && od < Array[i-1].OrderDate() ) Array[i] = Array[--i];
          Array[i] = temp;
       }
    }
    Not tested
  4. A generic approach is to templatize it and provide a comparator.
    Not compiled, not tested
    class OrderByDateAscending{
     public:
       bool IsLess(const Order& LHS, const Order& RHS){
          return LHS.OrderDate() < RHS.OrderDate();
       }
    }
    //////////////////////////////////////
    template <typename C, typename BinaryPredicate>
    void Sort(C* &Array[], int size, BinaryPredicate& comp){
       for(int i=1; i < size; ++i){
          C* temp = Array[i];
          while(i>0 && comp.IsLess(temp, Array[i-1]) ) Array[i] = Array[--i];
          Array[i] = temp;
       }
    }
    Not compiled, not tested
 

thanks to all !

I will use quicksort !

 
MarcoNeri: I will use quicksort !
What quicksort? Alphabetic Index of MQL4 Functions (600+) - MQL4 forum
 
WHRoeder:
MarcoNeri: I will use quicksort !
What quicksort? Alphabetic Index of MQL4 Functions (600+) - MQL4 forum

This one:

 Not Tested

void QuickSort(Order* &arr[], uint left, uint right)
{

  uint i = left, j = right;
  Order* tmp;
  Order* pivot = arr[MathAbs((left + right) / 2)];

  /* partition */

  while (i <= j) {
        while (arr[i].GetOrderOpenTime() < pivot.GetOrderOpenTime())
              i++;
        while (arr[j].GetOrderOpenTime() > pivot.GetOrderOpenTime())
              j--;

        if (i <= j) {
              tmp = arr[i];
              arr[i] = arr[j];
              arr[j] = tmp;
              i++;
              j--;
        }
  }

  /* recursion */

  if (left < j)
     QuickSort(arr, left, j);

  if (i< right)
     QuickSort(arr, i, right);

}

 

 Not tested

 
  1. What part of using SRC was unclear?
  2. Unless you have more than 16 orders, the Insertion sort will be faster.
 

Sorry for using of SRC :

 

class Dummy
{
};

 Now I get it. :-D 

Thank you again. 

Reason: