Sort multiple arrays

 

Hi, I have three arrays:

 

  • "Pair1"
  • "Pair2"
  • "Profit"

For example Pair1{EURUSD,GBPUSD}  / Pair2{AUDUSD,NZDUSD} / Profit{100,300}

These values are linked, so an output could be:

"Index 0: EURUSD-AUDUSD-100"

"Index 1: GBPUSD-NZDUSD-300".  

 Now I would sort a single array and then I would that other 2 arrays will remain linked to the sorted array.

So for example if I want to sort the third array "Profit" (desc) it will be {300,100}. Now pair1 and pair2 array should be {GBPUSD,EURUSD} and {NZDUSD,AUDUSD}. How can i do this in mql4?

Sorry for english, it's not my language. I hope it's enough clear.

Thanks for your help! 

 

2 ways

long buffers_index_copy[];
long buf_index_copy3[];
ArrayResize(buffers_index_copy,symols_chek_counter);
ArrayResize(buf_index_copy3,symols_chek_counter);
ArrayCopy(buffers_index_copy,volumes);
ArrayCopy(buf_index_copy3,volumes);
ArraySort(volumes,WHOLE_ARRAY,0,MODE_DESCEND);




for(int i=0;i<symols_chek_counter;i++)
{
if(volumes[i]==0) continue;
for(int bs=0;bs<symols_chek_counter;bs++)
{
if(volumes[i]==buffers_index_copy[bs])
{
...
buffers_index_copy[bs]=0;
break;
}
}
}

      

ArrayCopy(volumes,buf_index_copy3);

 

2)

 Asort[ 40, 2]

 Asort[ i, 0]=values

 Asort[ i, 1]=index in start arrays

 
Thanks eevviill, can you insert some comments to explain what you did? I need to sort also string arrays so I can't use always ArraySort function
 
Then you'd have write a specific sort, Array or not to array (Mike Tanton) - MQL4 forum - Page 2 #4
 
WHRoeder:
Then you'd have write a specific sort, Array or not to array (Mike Tanton) - MQL4 forum - Page 2 #4

Thanks, do you mean to sort string arrays?

If someone could do an example in mql4 like I did with simple text in my post, I will really appreciate it

 
The insertion sort I posted sorts any native type, and with a defined operator<() or a predicate any type. You would know this had you bothered to read the link provided.
 
WHRoeder:
The insertion sort I posted sorts any native type, and with a defined operator<() or a predicate any type. You would know this had you bothered to read the link provided.

I read but I did not understand anything that's why I asked if it was possible a concrete example with the data that I put in the first post. In any case, thanks for the link.

You posted a mqh file with some functions that I don't understand, sorry. But I have to use "insertion_sort" function with which parameters?

 

If I have 5 string arrays and 5 double arrays, if I do this:

insertion_sort(MyStringArray,0,ArraySize(MyStringArray),MyFilterArray);

 I have an overload error.

 
sky90:
Thanks eevviill, can you insert some comments to explain what you did? I need to sort also string arrays so I can't use always ArraySort function

 

////////////////////////////////////
void OnStart()
  {
  double numbs[6][2];
  string names [6];
  numbs[0][0]=3;numbs[1][0]=4.2;numbs[2][0]=2;numbs[3][0]=12.8;numbs[4][0]=1;numbs[5][0]=9;//value
  numbs[0][1]=0;numbs[1][1]=1;numbs[2][1]=2;numbs[3][1]=3;numbs[4][1]=4;numbs[5][1]=5;//start indexies
names[0]="a";names[1]="b";names[2]="c";names[3]="d";names[4]="e";names[5]="f";//names


  
ArraySort(numbs);
int index;
  
  for(int i=0;i<6;i++)
  {
  index=int(numbs[i][1]);
  Alert("Value:"+string(numbs[i][0])+" Name:"+names[index]);
  }
  
  
  }

numbs[i][1]

Will have start index. So you can use it for detect all you need. 

 
Or you can use Structures
 
sky90: I read but I did not understand anything that's why I asked if it was possible a concrete example with the data that I put in the first post.

I posted a link that showed you that "it was possible" and it has a "concrete example." All you had to do was read, understand, and adapt to your case.

Not compiled, not tested.

struct Data{
   string pair1;
   string pair2;
   double profit;
   void   Data(void){}                   // Allow array of Data
   void   Data(const Data& that) :       // Copy
      pair1(that.pair1), pair2(that.pair2), profit(that.profit){}
   void   operator=(const Data& that){   // Assignment
      pair1 = that.pair1; pair2 = that.pair2; profit = that.profit;
   }
   bool operator<(const Data& that){ return this.pair1 < that.pair1; }   // Default ordering      (Option A)

}
class DataByProfit{ public:                                              // Non-default ordering  (Option B)
  bool is_before(const Data& lhs, const Data& rhs){ return lhs.profit < rhs.profit; }
};
//////
Data data[]; ...; insertion_sort(data);                                  // sort by pair1.        (Option A)
DataByProfit obp; insertion_sort(data, 0 , ArraySize(data), obp);        // sort by profit.       (Option B)

Not compiled, not tested.

Was that so hard?