[ACCEPTED]-How to Sort a List<> by a Integer stored in the struct my List<> holds-sorting
Accepted answer
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.
var sortedList = yourList.OrderBy(x => x.Score);
or use OrderByDescending
to sort in opposite way
0
Use LINQ:
myScores.OrderBy(s => s.Score);
Here is a great resource to learn about 1 the different LINQ operators.
List<Highscore> mylist = GetHighScores();
var sorted = mylist.OrderBy(h=>h.Score);
0
This will sort the list with the highest 1 scores first:
IEnumerable<Highscore> scores = GetSomeScores().OrderByDescending(hs => hs.Score);
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.