C# Ranking of objects, multiple criteria

前端 未结 5 881
日久生厌
日久生厌 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:07

    This should work for a non-dense rank:

    static class Program
    {
    
        static IEnumerable GetResults(Dictionary wins, Dictionary scores)
        {
            int r = 1;
            double lastWin = -1;
            double lastScore = -1;
            int lastRank = 1;
    
            foreach (var rank in from name in wins.Keys
                                 let score = scores[name]
                                 let win = wins[name]
                                 orderby win descending, score descending
                                 select new Result { Name = name, Rank = r++, Score = score, Win = win })
            {
                if (lastWin == rank.Win && lastScore == rank.Score)
                {
                    rank.Rank = lastRank;
                }
                lastWin = rank.Win;
                lastScore = rank.Score;
                lastRank = rank.Rank;
                yield return rank;
            }
        }
    }
    
    class Result
    {
        public TournamentTeam Name;
        public int Rank;
        public double Score;
        public double Win;
    }
    

提交回复
热议问题