c# - 基于多个字段对对象进行排名

标签 c# linq sorting

<分区>

我有一个场景,我想根据不同的属性对记录列表进行排序和排名。每个对象都有一个Total 属性 以及一个名为Scores 的整数值列表, 模型就像

public class Routine
{
    public string Title { get; set; }

    public int Total { get; set; }

    public List<int> Scores { get; set; }
}

我要对记录进行排名,

  • 第一:基于总属性(property)
  • 第二个:基于Scores 属性中的第一个值
  • 然后:根据 Scores 属性中的第二个值
  • 等等

例如我考虑这三个记录。

enter image description here

感谢您的帮助和想法。

最佳答案

使用 linq,您可以使用 Orderby(Descending),然后将任意数量的 ThenBy(Descending) 子句链接在一起。

        var soterdList= initialList.OrderByDescending(r => r.Total)
            .ThenByDescending(r => r.Scores.FirstOrDefault())
            .ThenByDescending(r => r.Scores.Skip(1).FirstOrDefault()).ToList();

关于c# - 基于多个字段对对象进行排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41232915/

相关文章:

c# - 在具有 Entity Framework 奇怪行为的循环中构建查询

c# - foreach 循环似乎很慢

c# - 在 C# 中检测字符串中的重复单词时遇到问题?

java - Java多因子加权排序的高效实现

在 Lua Redis 中使用下划线排序

c# - 当不存在 MBN 设备时,来自 IMbnInterfaceManager::GetInterfaces 的结果

c# - WPF 中命令绑定(bind)的默认参数是什么?

c# - ADO.NET 实体模型更新

c# - Linq - 从基于 "weight"和 "order"的列表中获取项目

c# - 二变量排序算法