Starting C# by sorting a list of dictionary seems a bit bold, but anyway :
You need to use the Sort function. You need a function to compare two dictionary (I will assume it’s a <string, int> dictionary) :
private static int CompareDictionariesByScore (Dictionary<string, int> x,
Dictionary<string, int> y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two dictionaries.
int xScore = x[ "Score" ];
int yScore = y[ "Score" ];
if( xScore == yScore ) return 0;
else if( xScore > yScore ) return 1;
else return -1;
}
}
}
Thanks for all the suggestions - in the end I simplified it. Dictionaries were overkill and I created a value object to store the players name and score. I then used OrderBy that was mentioned in an answer above to order on one of the vo properties.