I have a list of arrays. Each array consists of a score and a difficulty. Read in from a text file.
This is how I get the data and order it by the score in descending order.
// load highscores
public void LoadScores()
{
// loop through each line in the file
while ((line = file.ReadLine()) != null)
{
// seperate the score from the difficulty
lineData = line.Split(',');
// add each line to the list
list.Add(lineData);
}
// order the list by descending order
IOrderedEnumerable<String[]> scoresDesc = list.OrderByDescending(ld => lineData[0]);
}
Is it possible to add the a clause to the IOrderedEnumerable
so that it orders by the score in descending order where the difficulty is 1
?
Assuming the "difficulty" is the second item in the array:
You can sort it afterwards, but
Where
returns anIEnumerable<T>
, not anIOrderedEnumerable<T>
, so if you need it to be anIOrderedEnumerable<T>
then it's cleaner (and faster) to filter the list first:(this is where
var
eases some pain, since you aren't bound to the return type)