How to implement ranking system with int[]'s [C#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
How to implement ranking system [C#]

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:
Code:
// 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,
 
Last edited:
Something like the following should work, but note its untested. Just as clarification what this code 'should' do is for each total in the array, it should loop through all the other values in the array checking if they are lower than or equal to it, if it is this should lower the rank by one.
C#:
			int nTotal = 0;
			int nNextTotal = 0;
			int sMaxRank = nPlayersCount;
			int sCurrentRank = 0;

			for (int i = 0; i <= nPlayersCount; i++)
			{
				int curTotal = naTotals[i];
				int curRank = nPlayersCount;

				for(int j = 0; j <= nPlayersCount; j++)
				{
					if(i != j)
					{
						if(curTotal >= naTotals[j])
						{
							curRank-= 1;
						}	
					}
				}

				naRanking[i] = curTotal;
			}

On a side note, writing an algorithm to sort the array would be very easy if you store the values in an arraylist. I guess that would probably be a slightly neater way to go.
 
Last edited:
PlausiblyDamp:

I was going to use something like the following:
Code:
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];

This is ALMOST perfect, the only problem left is that it cannot handle TIE situations (two or more players with 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...

This 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? Any help, hints, or ideas would be greatly appreciated,


Cags: Your code has the same problem when it comes to TIEs - I need to ensure that everyone has a unique rank after running the function. (btw, I assume you meant (naRanking =curRank;) on the last line of your code)
 
Last edited:
Well done, you spotted my deliberate mistake, *Ahem*. I was going to ask you how you wished to handle ties, as that does complicated things abit. The solution I suggested works like golf so that if three people are on -10 then they are tied for first and the person on -9 comes fourth. By the sounds of it you want a table that works like Football (soccer for all you americans). So that there can only be one person in each position.

Now assuming the metaphor you used of teams is the one being used by your application I'm assuming you have other properties of a team rather than thier score. If this is the case then I would have thought the best way to proceed would be to have a 'Team' Class which has a 'Score' Property. You could then create an array of classes and simply sort by that Property.
 
Back
Top