Sort array by value

87

Hi guys,

I need to sort an array by values... or at least find the 2 minimun/maximun values of a range of data...

I have the array (series array) daLow[100] correctly populated with the last 100 Low.

This is what I have done so far:

```   int iLowest1, iLowest2, iHighest1, iHighest2;

iLowest1 = ArrayMinimum( daLows, (iRange/2)+iPeriod, iRange-iPeriod );
iLowest2 = ArrayMinimum( daLows, iPeriod, (iRange/2)-iPeriod );
iHighest1 = ArrayMaximum( daHighs, (iRange-iPeriod)/2, iRange-iPeriod );
iHighest2 = ArrayMaximum( daHighs, 0, (iRange-iPeriod)/2+iPeriod );

sObjName = sId+"Support";
ObjectMove( 0, sObjName, 0, daTimes[iLowest1], daLows[iLowest1] );
ObjectMove( 0, sObjName, 1, daTimes[iLowest2], daLows[iLowest2] );

sObjName = sId+"Resistence";
ObjectMove( 0, sObjName, 0, daTimes[iHighest1], daHighs[iHighest1] );
ObjectMove( 0, sObjName, 1, daTimes[iHighest2], daHighs[iHighest2] );```

obviously daLows, daHighs and daTimes is correctly populated but as you can see this is not an elegant way to work.

How to improve this code?

"read all the array and gimme the two minimun/maximun values (and index)"

Moderator
10578

Mindexperiment:

Hi guys,

How to improve this code?

"read all the array and gimme the two minimun/maximun values (and index)"

Hi Mindexperiment,

Have you seen the function ArraySort

Documentação sobre MQL5: Funções para Array / ArraySort
• www.mql5.com
Funções para Array / ArraySort - Referência sobre algorítimo/automatização de negociação na linguagem para MetaTrader5
87

Yess but

```   ArraySort( daLows );
ArrayResize( daLows, 2 );

sObjName = sId+"Support";
ObjectMove( 0, sObjName, 0, daTimes[14], daLows[1] );
ObjectMove( 0, sObjName, 1, daTimes[52], daLows[0] );
```

I miss the index for the time

How can I  get the low index?

87

I wrote this function

```//+------------------------------------------------------------------+
//|                                            ArraySortByValues.mqh |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+

double ArraySortByValues( double &A[] ) {
double B[];
double R[2][2];

int size = ArraySize( A );
ArrayCopy( B, A );
ArraySort( B );

for ( int i=0; i<size; i++ ) {
if ( A[i] = B[0] ) {
R[0][0] = i;
R[0][1] = A[i];
}
if ( A[i] = B[1] ) {
R[1][0] = i;
R[1][1] = A[i];
}
}

return R;
}   ```

I'm having error on "return R"....... probably this is not the right sintax, which is the right one?

1045

Mindexperiment:

I wrote this function

I'm having error on "return R"....... probably this is not the right sintax, which is the right one?

You can not return arrays.
87

Ok, without array

```//+------------------------------------------------------------------+
//|                                            ArraySortByValues.mqh |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+

struct strTline {
double dLeftPrice;
int iLeftIndex;
double dRightPrice;
int iRightIndex;
};

strTline ArraySortByValues( double &A[] ) {
double B[];
strTline tLine;

int size = ArraySize( A );
ArrayCopy( B, A );
ArraySort( B );

for ( int i=0; i<size; i++ ) {
if ( A[i] == B[0] ) {
tLine.iLeftIndex = i;
tLine.dLeftPrice = A[i];
}
if ( A[i] == B[1] ) {
tLine.iRightIndex = i;
tLine.dRightPrice = A[i];
}
}

return tLine;
}```

Now I have this error:

"possible use of uninitialized variable 'tLine'    ArraySortByValues.mqh    35    11" on "return tLine;"

1045

Mindexperiment:

Ok, without array

Now I have this error:

"possible use of uninitialized variable 'tLine'    ArraySortByValues.mqh    35    11" on "return tLine;"

Rid of the warning, you can:

``` //+------------------------------------------------------------------+
//|                                            ArraySortByValues.mqh |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+

struct strTline {
double dLeftPrice;
int iLeftIndex;
double dRightPrice;
int iRightIndex;
};

strTline ArraySortByValues( double &A[] ) {
double B[];
strTline tLine={0};

int size = ArraySize ( A );
ArrayCopy ( B, A );
ArraySort ( B );

for ( int i= 0 ; i<size; i++ ) {
if ( A[i] == B[ 0 ] ) {
tLine.iLeftIndex = i;
tLine.dLeftPrice = A[i];
}
if ( A[i] == B[ 1 ] ) {
tLine.iRightIndex = i;
tLine.dRightPrice = A[i];
}
}

return tLine;
}```