C# Ranking of objects, multiple criteria

前端 未结 5 874
日久生厌
日久生厌 2020-12-05 12:26

I am building a plugin for a LAN party website that I wrote that would allow the use of a Round Robin tournament.

All is going well, but I have some questions about

5条回答
  •  爱一瞬间的悲伤
    2020-12-05 13:00

    I realize I'm late to the party, but I wanted to take a shot anyhow.

    Here is a version which uses LINQ exclusively:

    private IEnumerable GetRankings(Dictionary wins, Dictionary scores)
    {
        var overallRank = 1;
    
        return
            from team in wins.Keys
            group team by new { Wins = wins[team], TotalScore = scores[team] } into rankGroup
            orderby rankGroup.Key.Wins descending, rankGroup.Key.TotalScore descending
            let currentRank = overallRank++
            from team in rankGroup
            select new TeamRank(team, currentRank, rankGroup.Key.Wins, rankGroup.Key.TotalScore);
    }
    

    The return type:

    public class TeamRank
    {
        public TeamRank(TournamentTeam team, int rank, double wins, double totalScore)
        {
            this.Team = team;
            this.Rank = rank;
            this.Wins = wins;
            this.TotalScore = totalScore;
        }
    
        public TournamentTeam Team { get; private set; }
    
        public int Rank { get; private set; }
    
        public double Wins { get; private set; }
    
        public double TotalScore { get; private set; }
    }
    

提交回复
热议问题