# Sorting array variables in desc or asc order

65

Hi,

In each cycle I got different variables that stands for USD_strenght,GBP_strenght,JPY_strenght,CHF_strenght,EUR_strenght for example. These variables are numeric(integer) and can be form - to +

I want to sort these five variables in order of the value that consist that variable for example x=2, q=5,w=-4,e=0,r=-5 and sorted would be-5,-4,0,2,5.

I tried put these variables in to array and then do the sorting.

```int num_array[5]= {USD_strenght,GBP_strenght,JPY_strenght,CHF_strenght,EUR_strenght};
ArraySort(num_array);
ArraySort(num_array,WHOLE_ARRAY,0,MODE_DESCEND); // here I got the sorted array for example ```

But it says that in the first line coma or semicol is expected. Is there any other way to do what I want ? Or I have misunderstood the way hoe to insert data in one dimensional array?

Thanks :)

5390

You cannot put variable into array on declare in mql4 (works for constants/strings only). Use something like this instead.

• int num_array[5];
• num_array[0]=USD_strenght;
• num_array[1]=GBP_strenght;
• etc
65

ubzen:

You cannot put variable into array on declare in mql4 (works for constants/strings only). Use something like this instead.

• int num_array[5];
• num_array[0]=USD_strenght;
• num_array[1]=GBP_strenght;
• etc

Thanks it worked :)

But now how can I know what was that value? How can I know after sorting that array which of these 5 numeric values is USD_strenght or GBP_strenght and so on ?

It is very importat to know that information.

If You don't know, then I need other solution for this situation that I need to compare these 5 variables and determinate their strenght? (max,min ) without puting these values in array and sorting later ?

Thanks

379

edgars.rworks:

Thanks it worked :)

But now how can I know what was that value? How can I know after sorting that array which of these 5 numeric values is USD_strenght or GBP_strenght and so on ?

It is very importat to know that information.

If You don't know, then I need other solution for this situation that I need to compare these 5 variables and determinate their strenght? (max,min ) without puting these values in array and sorting later ?

Thanks

How about something like this:

```// lets assume that:
int USD_strength = 9, GBP_strength = 6, JPY_strength = 7, CHF_strength = 5, EUR_strength = 8;

// create and initialize arrays
string currencies[] = {"USD", "GBP", "JPY", "CHF", "EUR"};
int num_array[5][2]; ArrayInitialize(num_array, 0);

// put data in num_array and sort
//    * as you can see, num_array[x][1] holds the index for the appropriate currency described in currencies[]
num_array[0][0] = USD_strength; num_array[0][1] = 0;
num_array[1][0] = GBP_strength; num_array[1][1] = 1;
num_array[2][0] = JPY_strength; num_array[2][1] = 2;
num_array[3][0] = CHF_strength; num_array[3][1] = 3;
num_array[4][0] = EUR_strength; num_array[4][1] = 4;
ArraySort(num_array);

// print results
for (int i = 0; i < ArrayRange(num_array, 0); i++)
Print ("The strength for ", currencies[num_array[i][1]], " is ", num_array[i][0]);```

The above code produces the following in the journal/log (in the Terminal):

The above code produces the following in the journal/log (from the actual log file):

As you can see, you can associate the sorted strength by using the second dimension of num_array as an index to the currencies[] array.

Hope this sheds a little light on the solution which you are looking for.

16112

edgars.rworks: But now how can I know what was that value? How can I know after sorting that array which of these 5 numeric values is USD_strenght or GBP_strenght and so on ?

You can't. You have to handle that with your own code. Perhaps:

not compiled, not tested
```void ArraySort2D(double&   array[][],  int eSel=0, int count=WHOLE_ARRAY,
int       start=0,    int sort_dir=MODE_ASCEND){
int   asc = +1;   if(sort_dir != MODE_ASCEND)   asc = -1;
int   size2nd  = ArrayRange(array,1),
limit    = start + count;
if(count == WHOLE_ARRAY)   limit = ArrayRange(array,0);
double temp[]; ArrayResize(temp, size2nd);
for(int iUnsorted = start+1; iUnsorted < limit; iUnsorted++){
for(int i=0; i < size2nd; i++)   temp[i] = array[iUnsorted][i];
for(int iEmpty = iUnsorted; iEmpty > start; iEmpty--){   // Insertion sort
if((array[iEmpty - 1][eSel] - temp[eSel])*asc < 0.) break;
for(i=0; i < size2nd; i++) array[iEmpty][i] = array[iEmpty - 1][i];
}
for(i=0; i < size2nd; i++) array[iEmpty][i] = temp[i];   // Insert.
}
}
//////////////
#define USD 0
#define GBP 1
#define nCUR 2
string curToText[]={"USD", "GBP"};
#define STR 0
#define CUR 1
#define nVAL 2

int array[nCUR][nVAL];
array[USD][STR]=USD_strenght; array[USD][CUR]=USD;
array[GBP][STR]=GBP_strenght; array[GBP][CUR]=GBP;

ArraySort2D(array, STR);
double maxStr = array[nCUR-1][STR]; int eMaxCur = array[nCUR-1][CUR]
Print("Max strength = "+ maxStr + " (" + curToText[eMaxCur] +")");```
not compiled, not tested
16112

Thirteen: How about something like this:
```int num_array[5][2]; ArrayInitialize(num_array, 0);
ArraySort(num_array);```
The above code produces IRRELEVANT
No where does the doc says ArraySort takes a 2D array. I'd do some more testing to be sure.
379

WHRoeder:

Thirteen: How about something like this:

```int num_array[5][2]; ArrayInitialize(num_array, 0);
ArraySort(num_array);```

The above code produces IRRELEVANT

No where does the doc says ArraySort takes a 2D array. I'd do some more testing to be sure.

(Original Response: No where does the doc says ArraySort takes a 2D array and your output shows the same thing as input. If the sort has worked USD (the largest) would be listed LAST (ascending order).)

The documentation for ArraySort() states: "Sorts numeric arrays by first dimension." This, at the very least, leaves room for the possibility that a ArraySort() can take a 2D array, which I see is possible through testing.

```// lets assume that:
int USD_strength = 9, GBP_strength = 6, JPY_strength = 7, CHF_strength = 5, EUR_strength = 8;

// create and initialize arrays
string currencies[] = {"USD", "GBP", "JPY", "CHF", "EUR"};
int num_array[5][2]; ArrayInitialize(num_array, 0);

// put data in num_array and sort
//    * as you can see, num_array[x][1] holds the index for the appropriate currency in currencies[]
num_array[0][0] = USD_strength; num_array[0][1] = 0;
num_array[1][0] = GBP_strength; num_array[1][1] = 1;
num_array[2][0] = JPY_strength; num_array[2][1] = 2;
num_array[3][0] = CHF_strength; num_array[3][1] = 3;
num_array[4][0] = EUR_strength; num_array[4][1] = 4;

string text = "Unsorted: ";
for (int j = 0; j < ArrayRange(num_array, 0); j++)
text = text + currencies[num_array[j][1]] + " has a strength of " + num_array[j][0] + ", ";
text = StringSubstr(text, 0, StringLen(text)-2);   //removing last comma
Print (text);

ArraySort(num_array);
text = "Sorted (ascending): ";
for (j = 0; j < ArrayRange(num_array, 0); j++)
text = text + currencies[num_array[j][1]] + " has a strength of " + num_array[j][0] + ", ";
text = StringSubstr(text, 0, StringLen(text)-2);   //removing last comma
Print (text);

ArraySort(num_array, WHOLE_ARRAY, 0, MODE_DESCEND);
text = "Sorted (decending): ";
for (j = 0; j < ArrayRange(num_array, 0); j++)
text = text + currencies[num_array[j][1]] + " has a strength of " + num_array[j][0] + ", ";
text = StringSubstr(text, 0, StringLen(text)-2);   //removing last comma
Print (text);```

So, what is your complaint about my code or about my use of ArraySort()? Also, how are the above results any different than the results that I showed in my previous post? (and I hope you remember that the journal in the Terminal is displayed in reverse order--i.e., newest first.)

BTW, it was alluded to here and here that ArraySort() might be able to take a 2D array.

5390

edgars.rworks:

Thanks it worked :)

But now how can I know what was that value? How can I know after sorting that array which of these 5 numeric values is USD_strenght or GBP_strenght and so on ?

It is very importat to know that information.

If You don't know, then I need other solution for this situation that I need to compare these 5 variables and determinate their strenght? (max,min ) without puting these values in array and sorting later ?

Thanks

Your best solution involves arrays and the guys provided good examples above. I just wanted to give an alternative example.

```#define WRONG_VALUE -1

double  USD_strenght=1;
double  GBP_strenght=2;
double  JPY_strenght=3;
double  CHF_strenght=2;
double  EUR_strenght=1;

void start(){
}

string VariableOfGreatestValue(){
double  Greatest_Value=     0.0;
int     IndexOfGreatest=    WRONG_VALUE;
for(int i=0; i<5; i++){
if(ReturnIndexValue(i)<=Greatest_Value) continue;
Greatest_Value=ReturnIndexValue(i);
IndexOfGreatest=i;
}
if(IndexOfGreatest==WRONG_VALUE) return("");
return( VariableNameOfIndex(IndexOfGreatest) );
}

string VariableNameOfIndex(int Index){
switch(Index){
case 0:  return("USD_strenght");
case 1:  return("GBP_strenght");
case 2:  return("JPY_strenght");
case 3:  return("CHF_strenght");
case 4:  return("EUR_strenght");
default: return("");
}
}

double ReturnIndexValue(int Index){
switch(Index){
case 0:  return(USD_strenght);
case 1:  return(GBP_strenght);
case 2:  return(JPY_strenght);
case 3:  return(CHF_strenght);
case 4:  return(EUR_strenght);
default: return(WRONG_VALUE);
}
}```
16112

Thirteen: The documentation for ArraySort() states: "Sorts numeric arrays by first dimension."
I stand corrected. (My insertion sort sorts by any dimension).
3

I need to sort iexposure in Declining order according to Net Lots Size.

Any Help would be appreciated.

Thanks

16112

 manish_vibhs: I need to sort iexposure in Declining order according to Net Lots Size.Any Help would be appreciated. learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem. No such function iexposure