How to create rank function for some value's? Need Help

 

Anybody can help me how to create rank function

for example I have some double value's like theese:

a=3.0; b=4.0; c=5.0; d=2.0; e=1.0; f=6.0; g=0.0;

from higher to lower value (6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0)

or the value of (f,c,b,a,d,e,g)

So the rank's should be like this

rank number: (th)

f=1 ; c=2; b=3; a=4; d=5; e=6; g=7;

What I need is a function that return the rank if I input the value

int rank (double value)

{

process all value's;

return (the rank of a value from all value's)

}

I know it is easy if we just make a function like this

for example value of "a"

int rank (double value)

{

if(a>b && a>c && a>d && a>e && a>f && a>g)

return (1);

.

.

.

if(a<b && a<c && a<d && a<e && a<f && a<g)

return(7);

}

The Problem is when the variations of value are 2 the combination only a few, but when the variations increase for example 5,7 or 10 etc so the combination to make the rank will very very a lot..

Hope somebody can help me for this kind of problem..

Thank's berfore, Sorry for my poor english, but I'm learning..

Pj..

 

if you place your values in an array then sort the array in descending order using ArraySort function, the index of the array + 1 is will be the rank.

int rank (double value)

{

double All_Values[] = {3.0,4.0,5.0,2.0,1.0,6.0,0.0};

ArraySort(All_Values,WHOLE_ARRAY,0,MODE_DESCEND);

// The above 2 lines probably should be elsewhere and set as global variables

for (int i=0; i<ArraySize(All_Values); i++)

{

if (All_Values == value)

{

return (i+1);

}

}

return (0);

}

hope this helps.

 

Rank Array Function - Generic

primajaya:
Anybody can help me how to create rank function

for example I have some double value's like theese:

a=3.0; b=4.0; c=5.0; d=2.0; e=1.0; f=6.0; g=0.0;

from higher to lower value (6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0)

or the value of (f,c,b,a,d,e,g)

So the rank's should be like this

rank number: (th)

f=1 ; c=2; b=3; a=4; d=5; e=6; g=7;

What I need is a function that return the rank if I input the value

int rank (double value)

{

process all value's;

return (the rank of a value from all value's)

}

Pj..

This is a useful function to have around. I always intended to write a generic function for this, so here goes.

I am assuming you have an array with some values, but you want to keep the original array in the same order. I have created a function

RankArrray() that will return an array that points to the original array in ranked order. Default is descending order, ascending is option.

Since I choose not to change the original array, I return the result in an Array "Rank[], each value of which is the index of the value in the Input Array in ranked order.

As written the original input array must be single dimension, if you have an array with more than one dimension you need to modify the function or copy to a single dimension array first.

I include an example of how to test it and how to use the RankArray to point to the original in rank order. Drop the example in your expert folder, complile and attach to a chart - examine the experts log.

Try it out and see how it works.

//+-------------------------------------------------------------

void RankArray(double InputArray[], int &Ranked[], bool sort_descending=true) {

//Purpose: Input: an array with a set of numbers that have been calculated in some fashion

// Output is array Ranked[] such that

// Ranked[0] points to the first member of the InputArray[]

// i.e. InputArray[Ranked[0]] is highest value

// InputArray[Ranked[1]] is next highest, ect.

// InputArray[] is unchanged

// sort_descending optional, default value true - is for Descending Order

// false will sort in ascending order

// User can modify as necessary for two dimensional Arrays.

int InputArraySize=ArrayRange(InputArray,0);

// Create Temporary array to sort - with pointers to original element

double TempArray[1][2];

ArrayResize(TempArray,InputArraySize);

for (int i=0;i<InputArraySize;i++)

{TempArray[0]=InputArray;

TempArray[1]=i;

}

int sort_dir;

//Validate input values

if (!sort_descending) sort_dir=MODE_ASCEND;

else sort_dir=MODE_DESCEND;

//Sort the Array-first element only

ArraySort(TempArray,WHOLE_ARRAY,0,sort_dir);

//Create output array - ranked by pointing to orignal values in ranked order

ArrayResize(Ranked,InputArraySize);

for (i=0;i<InputArraySize;i++)

{Ranked=TempArray[1];}

return;

}

Files:
test_sort.mq4  2 kb
Reason: