ArraySort() error on multidimensional array - page 4

 
amrali #:

You could specialize the 'generic' binary search function for your needs like this:

I got the solution from ChatGPT:

   int index = MarkerArrayBinarySearch(MarkerArray, CueLinePos);
   if(index >= 0)
   {
      Print("Index of the found element: ", index);
      Print("Current CueLinePos value: ", TimeToString(CueLinePos));
//      Print(MarkerArray[index]);

      const MarkerData foundMarker = MarkerArray[index];
      Print("Found MarkerData: Timestamp=", TimeToString(foundMarker.timestamp), " Flag=", foundMarker.flag);

   }
   else
   {
      Print("Element not found!");
   }

Which shows me that I was trying to access the array record improperly, I should have addressed the timestamp and flag values instead. Will test out to see wether if I really need the extra variable or not, and I also found that the function gave 0 result always because I called it standing on the last entered record, which, after the sort became index 0, when I added a different value, it returned the correct index.

Thank you very much for helping me out @amrali, @Dominik Egert, @Alain Verleyen!

I consider this question is solved.

 
I am happy that you managed to solve it. But remember that sorting and binary search are faster when you have > 100 records in your array. For smaller arrays these solutions will not be of much benefit 
 
J.P.Satrio #:

I got the solution from ChatGPT:

Which shows me that I was trying to access the array record improperly, I should have addressed the timestamp and flag values instead. Will test out to see wether if I really need the extra variable or not, and I also found that the function gave 0 result always because I called it standing on the last entered record, which, after the sort became index 0, when I added a different value, it returned the correct index.

Thank you very much for helping me out @amrali, @Dominik Egert, @Alain Verleyen!

I consider this question is solved.

I also realized that the topic title was also misleading. It should have been called: MQL5 standard library ArraySort() does not work on arrays with struct.

 
amrali #:
I am happy that you managed to solve it. But remember that sorting and binary search are faster when you have > 100 records in your array. For smaller arrays these solutions will not be of much benefit 

This is a part of a larger project, which requires large chunks of data (>1000 records), so I'm quite sure this will be the best solution. Thanks again!

 
amrali #:
Actually this is a binary search. One of many other variations. Variations include getting the nearest match if exact item is not found, next greater or lesser element and etc .
I see. I thought binary search always involves halfing the search area on each iteration until you have found the element, or keep bouncing between the two nearest...

Learned something new.
 
Alain Verleyen #:

A database for 1000 objects ? Not a good idea.

Just use a hashmap with the timeframe/timestamp as a key. You will find a library in Include/Generic.

In addition if your code is doing a lot of additions and deletions to the array, a hash map or binary search tree (like a red-black tree) will be the best options here for your case.
 
amrali #:

You could specialize the 'generic' binary search function for your needs like this:

By the way, is it possible to make the binary search field (type) independent to generalize it for future use? I've added an integer to the struct, and currently I am only able to search it with a for loop.

ChatGPT recommended these modifications:

template<typename T, typename V>
int ArrayBinarySearchByField(const T &array[], const V &value, V T::*field)
{
    int lo = 0;
    int hi = ArraySize(array) - 1;

    while (lo <= hi)
    {
        int i = lo + (hi - lo) / 2;

        if (array[i].*field < value)
        {
            lo = i + 1;
        }
        else if (value < array[i].*field)
        {
            hi = i - 1;
        }
        else
        {
            return i;
        }
    }

    return -1; // not found
}

But the compiler recognized V as a variable and was demanding a type for it (I'm not good in reading/implementing/transforming C++ syntax, terminology - the bot said it allows typeless definitions).

 
J.P.Satrio #:

By the way, is it possible to make the binary search field (type) independent to generalize it for future use? I've added an integer to the struct, and currently I am only able to search it with a for loop.

ChatGPT recommended these modifications:

But the compiler recognized V as a variable and was demanding a type for it (I'm not good in reading/implementing/transforming C++ syntax, terminology - the bot said it allows typeless definitions).

Well, that is pointer arithmetic, not supported in MQL as of today.

It is impossible to write such code in MQL, you need a different approach.
 
Dominik Egert #:
Well, that is pointer arithmetic, not supported in MQL as of today.

It is impossible to write such code in MQL, you need a different approach.

I see. Sad to hear. Thanks!

 
J.P.Satrio #:

By the way, is it possible to make the binary search field (type) independent to generalize it for future use? I've added an integer to the struct, and currently I am only able to search it with a for loop.

Use the generic function as it is, and customize the "Less" function according to the desired structure fields.