Click to See Complete Forum and Search --> : How to implement ranking system with int[]'s [C#]


Shaitan00
March 2nd, 2006, 08:35 PM
I am trying to create a function/algorithm/method in C# to implement a ranking system.
I have an array (naTotals[]) that contains the Total Score for each team (where the team number is represented by the arrays index value)

I need to find a way to determine the RANKING of each team given their Total Score and store their RANK NUMBER in another array (naRankings[] - where the team number is represented by the arrays index value)
So, for example - if naTotals[2] = (20, 5, 15) then after running the function/algorithm/method naRankings[2] should = (0, 2, 1) corresponding to the RANK of each team where 0 is the BEST and 2 the WORST rank (since Team0 had a Total of 20 it was Rank0 as the Best)

This is what I have / my attempt so far:

// Determine Ranking
int[] naRanking = new int[nPlayersCount];
for (int i = 0; i <= nPlayersCount; i++)
naRanking[i] = 0;

int nTotal = 0;
int nNextTotal = 0;
int sMaxRank = nPlayersCount;
int sCurrentRank = 0;
for (int i = 0; i <+ nPlayersCount; i++)
{
nTotal = naTotals[i];
nNextTotal = naTotals[i+1];
if (nTotal >= nNextTotal)
{
// Somehow do something with naRanking
// Rank this one and hold the next for reranking on recompare with next index?
}
}

But as you can see this is far from complete/working - the more I look at it the more I realize it won't work, having a REAL HARD TIME coming up with a viable solution...

But of course I run into the obvious problem of how to save the nNext value before overwriting it and so forth...
There has to be a nice/better way to go about getting these results no? If not maybe some recursive funtion able to "order" the array somehow?

Any help, hints, or advice would be greatly appreciated
Thanks,

Athley
March 3rd, 2006, 09:40 AM
If it is ok for you to cast through a sortable typ of object you might be able to do something like this:

int[] naTotals = new int[3];
naTotals[0] = 20;
naTotals[1] = 5;
naTotals[2] = 15;

ArrayList a = new ArrayList(naTotals);
ArrayList b = (ArrayList)a.Clone();
b.Sort();
b.Reverse();

for (int i = 0; i <= (a.Count - 1); i++)
a[a.IndexOf(b[i])] = b.IndexOf(b[i]);

int[] naRankings = new int[naTotals.GetLength(0)];

for (int j = 0; j <= (a.Count - 1); j++)
naRankings[j] = (int)a[j];

for (int k = 0; k <= (naRankings.GetLength(0) - 1); k++)
Console.WriteLine(naRankings[k].ToString());

Try this in a colsoleapp and see what you think...

/Leyan

Shaitan00
March 4th, 2006, 02:18 AM
Athley: Almost perfect ... I am just having a real hard time handling situations where two or more Players have the same TOTAL.

Now, I don't care how this is resolved as long as at the end of the day there are no two teams that have the same Rank - for all I care we could randomely select which go where ... or the first one checked is always the highest or lowest rank, etc... anything easy to implement is perfectly fine-by-me...

Your code seems to have a hard time handling this case (things get messy when TIEs occur) and I have been utterly unsucessful at making the required changes - do you have any ideas?

Note - this is by far the best method I have seen to date, thanks a bunch.

Athley
March 4th, 2006, 03:20 AM
A slight modification of the code should do the trick.

From:
for (int i = 0; i <= (a.Count - 1); i++)
a[a.IndexOf(b[i])] = b.IndexOf(b[i]);
To:
for (int i = 0; i <= (a.Count - 1); i++)
a[a.IndexOf(b[i])] = i;
/Leyan