- Home /
Sorting a List of Dictionaries in C#?
Help!
I am new to C# and have spent all day trying to work out a way to do the following.
Read a XML file that contains a player's name and player's score and store it in a variable. I have been using a Dictionary.
Place all these dictionaries into a List.
Sort the list of dictionaries by the high score. (can't work this out)
Have the ability to add another new score and sort again.
Write the results back to the file I loaded it from.
Is the approach I'm taking wrong using a List of Dictionaries? Any advice would be a great help.
Thanks.
This is not really a Unity question, Stack Overflow would be a better forum for this.
Answer by jk15 · Jun 19, 2012 at 02:50 AM
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.
Answer by whydoidoit · Jun 18, 2012 at 03:37 PM
using System.Linq;
var sortedList = myListOfDictionaries.OrderBy(d=>d["yourScoreName"]).ToList();
I would also suggest you consider making classes that are X$$anonymous$$LSerialized. $$anonymous$$uch better than a dictionary:
public class $$anonymous$$yList
{
[XmlElement("NameOfThePlayerInfo")]
public PlayerInfo[] Info;
}
public class PlayerInfo
{
[XmlAttribute]
public string Name;
[XmlAttribute]
public int Score;
}
The use XmlSerializer to deserialize $$anonymous$$yList and it will contain a list of the players.
Answer by Berenger · Jun 18, 2012 at 03:16 PM
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 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;
}
}
}
Then, you can sort the list that way
list.Sort( CompareDictionariesByScore );
Answer by Wolfram · Jun 18, 2012 at 03:30 PM
Why not use a SortedDictionary instead?
Also, there's about a gazillion useful google hits for that:
http://stackoverflow.com/questions/289/how-do-you-sort-a-c-sharp-dictionary-by-value
http://stackoverflow.com/questions/5949904/c-sharp-sort-dictionary-with-linq
Your answer
Follow this Question
Related Questions
C# List Reported as Empty when called in method 1 Answer
From two lists into a dictionary... and back? 5 Answers
Sort list game objects based on name 3 Answers
Find index of item in list 1 Answer
Runtime instantiation based on XML 1 Answer