[ACCEPTED]-How to Sort a List<> by a Integer stored in the struct my List<> holds-sorting

Accepted answer
Score: 53

I don't know why everyone is proposing LINQ 6 based solutions that would require additional 5 memory (especially since Highscore is a 4 value type) and a call to ToList() if one 3 wants to reuse the result. The simplest 2 solution is to use the built in Sort method 1 of a List

list.Sort((s1, s2) => s1.Score.CompareTo(s2.Score));

This will sort the list in place.

Score: 6
var sortedList = yourList.OrderBy(x => x.Score);

or use OrderByDescending to sort in opposite way

0

Score: 1

Use LINQ:

myScores.OrderBy(s => s.Score);

Here is a great resource to learn about 1 the different LINQ operators.

Score: 0
List<Highscore> mylist = GetHighScores();

var sorted = mylist.OrderBy(h=>h.Score);

0

Score: 0

This will sort the list with the highest 1 scores first:

IEnumerable<Highscore> scores = GetSomeScores().OrderByDescending(hs => hs.Score);

More Related questions