C# 根据多个条件对列表进行排序

标签 c# sorting

我需要对一些足球排名进行排序。我的问题是如何以正确的顺序排序。

排序顺序:

  • 积分降序
  • 近似匹配
  • 净胜球DESC
  • 进球数 DESC
  • 进球数

输入: TeamName - Points - GoalsScored - GoalsAgainst

  • 团队 1 - 1 - 4 - 7
  • 2 - 5 - 8 - 6 队
  • 团队 3 - 1 - 2 - 10
  • 团队 4 - 8 - 12 - 5
  • 5 - 5 - 7 - 4 组

... 第 4 场比赛 - 第 5 队 - 第 2 队 -- 1-2

第 7 场比赛 - 第 1 队 - 第 3 队 -- 3-3 ...

输出: TeamName - Points - GoalsScored - GoalsAgainst

  • 团队 4 - 8 - 12 - 5
  • 2 - 5 - 8 - 6 队
  • 5 - 5 - 7 - 4 组
  • 团队 1 - 1 - 4 - 7
  • 团队 3 - 1 - 2 - 10

因为第 2 队战胜了第 5 队,他们最终获得了第二名。

因为第 1 队和第 3 队平局,他们最终以净胜球优势排名第 4。

public class Standing
{
    public Team Team { get; set; }
    public int? MatchesPlayed { get; set; }
    public int? GoalsScored { get; set; }
    public int? GoalsAgainst { get; set; }
    public int? Points { get; set; }
}

public class Match
{
    public int MatchID { get; set; }
    public DateTime? PlayTime { get; set; }
    public Team HomeTeam { get; set; }
    public Team AwayTeam { get; set; }
    public int? HomeScore { get; set; }
    public int? AwayScore { get; set; }
}

public class Pool
{
    public int PoolID { get; set; }
    public string PoolName { get; set; }
    public DateTime? StartTime { get; set; }
    public List<Team> Teams { get; set; }
    public List<Match> Matches { get; set; }
    public List<Standing> Standings { get; set; }
}

最佳答案

可以使用 .NET 3.5 吗?为此使用 LINQ OrderByThenBy 扩展方法非常简单。

关于C# 根据多个条件对列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1862881/

相关文章:

python - 如何按特定值对 Pandas 列进行排序

C qsort 不对结构数组进行排序

java - 插入排序算法大o : iterative and recursive

C# 用数字对数组列表进行排序

c# - C#版本、VS版本、.NET framework版本等如何搭配?

c# - 在当前控制台运行进程

c# - 如何在 C# 中将列添加到加载了过时架构的数据集中?

c# - 您使用什么(免费)格式化C#代码?

c# - Azure 存储表 : await table. ExecuteAsync(InsertOperation) 执行,但从未完成

python - 按字母顺序对键/值列表进行排序