How to sort an array and keep track of indexes ?

 

Hi!

Let's suppose I want to sort "valPair" and keep track of its corresponding "labelPair" index.

string   labelPair[5]=  { "AUDJPY","AUDUSD","CADJPY","CHFJPY","EURAUD" };
double   valPair[5];

for(int i=0; i<5; i++)
  valPair[i]= indicator(labelPair[i], parameters);

"ArraySort(valPair)" doesn't work because it replaces the "valPair" index and loses its corresponding "labelPair" index.

Can someone help?

Thanks very much!

 
ccou:

Hi!

Let's suppose I want to sort "valPair" and keep track of its corresponding "labelPair" index.

"ArraySort(valPair)" doesn't work because it replaces the "valPair" index and loses its corresponding "labelPair" index.

Can someone help?

Thanks very much!

To keep the binding between the label and value, you could store them in an array of structs or objects.

This dictionary object should work. There doesn't seem to be a sort() routine, though; so you would have to roll your own.

https://www.mql5.com/en/articles/1334#c3

Or using a struct:

https://www.mql5.com/en/forum/203831#comment_5273405

MQL5 Cookbook: Implementing an Associative Array or a Dictionary for Quick Data Access
MQL5 Cookbook: Implementing an Associative Array or a Dictionary for Quick Data Access
  • 2015.06.02
  • Vasiliy Sokolov
  • www.mql5.com
This article describes a class for convenient storage of information, namely an associative array or a dictionary. This class allows to gain access to information by its key. The associative array resembles a regular array. But instead of an index it uses some unique key, for example, ENUM_TIMEFRAMES enumeration or some text. It does not matter...
 

Thank you Anthony but your response is too difficult to understand, I'm a beginner programmer, moreover I'm using MQL4, not MQL5.

I would appreciate some practical help with a simple and clear example that works, thank you.

 
ccou:

Hi!

Let's suppose I want to sort "valPair" and keep track of its corresponding "labelPair" index.

"ArraySort(valPair)" doesn't work because it replaces the "valPair" index and looses its corresponding "labelPair" index.

Can someone help?

Thanks very much!

double   valPair[5][2];

valPair[1][0]....[1][4] point to LabelPair[0]...[4]

 

Can someone give a CLEAR AND SIMPLE MQL4 example that works?

Is it so difficult ??

Thanks

 
ccou:

Hi!

Let's suppose I want to sort "valPair" and keep track of its corresponding "labelPair" index.

"ArraySort(valPair)" doesn't work because it replaces the "valPair" index and loses its corresponding "labelPair" index.

Can someone help?

Thanks very much!

You could try making valPair a 2 dimensional array, store the value in the first dimension and the index for labelPair in the second.

Then you could sort the array and use the second dimension value to find which pair is being referenced.

 
Keith Watford:

You could try making valPair a 2 dimensional array, store the value in the first dimension and the index for labelPair in the second.

Then you could sort the array and use the second dimension value to find which pair is being referenced.

Excellent! Thank you