C#按多个条件排序列表

Fre*_*ets 2 c# sorting

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

排序:

  • 积分DESC
  • 近似匹配
  • 目标差异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队,他们最终获得了第2名.

因为第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; }
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*son 18

你能用.NET 3.5吗?为此,使用LINQ OrderByThenBy扩展方法非常简单.


No *_*rns 6

我认为您应该检查IComparable界面并考虑在您的对象上实现它.