1

我有一个数组列表。每个数组由一个分数和一个难度组成。从文本文件中读取。

这就是我获取数据并按分数降序排列的方式。

// 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]);

}

是否可以将 a 子句添加到 theIOrderedEnumerable中,以便在难度所在的位置按分数降序排列1

4

3 回答 3

2

假设“难度”是数组中的第二项:

 IEnumerable<String[]> scoresDesc =
     list.OrderByDescending(ld => lineData[0])
         .Where(ld => lineData[1] == 1);

可以在之后对其进行排序,但Where返回 an IEnumerable<T>,而不是 an IOrderedEnumerable<T>,因此如果您需要它是 anIOrderedEnumerable<T>那么首先过滤列表会更干净(更快):

 IOrderedEnumerable<String[]> scoresDesc =
     list.Where(ld => lineData[1] == 1)
         .OrderByDescending(ld => lineData[0]);

(这是var减轻一些痛苦的地方,因为您不受返回类型的约束)

于 2014-04-29T13:49:43.767 回答
0

如果要筛选难度为 1 的,可以使用.Where()扩展方法,如果要按多个字段排序,可以使用.ThenBy()扩展方法。

于 2014-04-29T13:39:52.330 回答
0

首先过滤然后订购:

list.Where(x => x.difficulty == 1).OrderByDescending(ld => lineData[0]);
于 2014-04-29T13:43:51.917 回答