我需要对一些足球排名进行排序。我的问题是如何按正确的顺序排序。
排序顺序:
输入: TeamName - 积分 - GoalsScored - GoalsAgainst
... 第4场比赛 - 第5队 - 第2队 - 1-2
第7场比赛 - 第1队 - 第3队--3-3 ...
输出: TeamName - 积分 - GoalsScored - GoalsAgainst
因为第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; }
}
答案 0 :(得分:18)
你能使用.NET 3.5吗?为此,使用LINQ OrderBy
和ThenBy
扩展方法非常简单。
答案 1 :(得分:6)
我认为您应该查看IComparable
界面并考虑在您的对象上实现它。
答案 2 :(得分:1)
如果您的数据存储在数据库中(是吗?),那么您可以使用ORDER BY对select查询进行排序。然后,生成的C#数据结构将按顺序排列。
答案 3 :(得分:0)
你想自己写一个比较器函数并将其传递给sort方法。
如果一支球队有更多积分,请表明该球队“更大”。
如果两支球队的得分数相同,请查看球门差异,并用它来决定哪支球队“更大”。
答案 4 :(得分:0)
简单易学
var newlist = sourcelist.OrderBy(x => x.Inactive).ThenBy(y => y.Description).ToList()