I need to sort a highscore file for my game I’ve written.
Each highscore has a Name, Score and Date variable. I store each one in a List.
Here is the struct that holds each highscores data.
struct Highscore
{
public string Name;
public int Score;
public string Date;
public string DataAsString()
{
return Name + "," + Score.ToString() + "," + Date;
}
}
So how would I sort a List of type Highscores by the score variable of each object in the list?
Any help is appreciated 😀
I don’t know why everyone is proposing LINQ based solutions that would require additional memory (especially since Highscore is a value type) and a call to ToList() if one wants to reuse the result. The simplest solution is to use the built in Sort method of a List
This will sort the list in place.