
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
You could specialize the 'generic' binary search function for your needs like this:
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 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.
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!
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 .
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.
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:
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).
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).
I see. Sad to hear. Thanks!
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.