微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#对象排名,多重条件

我正在建立一个我写的LAN聚会网站的插件,可以使用Round Robin锦标赛.

一切进展顺利,但我对有两种标准的最有效的方法提出了一些问题.

基本上,我想要以下排名布局:

Rank  Wins  Totalscore
PersonE  1     5     50
PersonD  2     3.5   37
PersonA  2     3.5   37
PersonC  4     2.5   26
PersonB  5     2.5   24
PersonF  6     0     12

sql Server中,我将使用:

SELECT
    [Person],RANK() OVER (ORDER BY Wins DESC,Totalscore DESC) [Rank],[Wins],[Totalscore]

现在,我只有List,Dictionary等才能使用

特别:

Dictionary<TournamentTeam,double> wins = new Dictionary<TournamentTeam,double>();
Dictionary<TournamentTeam,double> score = new Dictionary<TournamentTeam,double>();

有没有办法用LINQ做这样的排名风格?

如果没有,是否有一种可扩展的方式,让我以后能够参与Win-Loss-Draw的帐户,而不是仅仅赢得选择?

编辑:

我的适应TheSoftwareJedi的答案:

private class RRWinRecord : IComparable
{
    public int Wins { get; set; }
    public int Losses { get; set; }
    public int Draws { get; set; }
    public double Overallscore { get; set; }
    public double WinRecord
    {
        get
        {
            return this.Wins * 1.0 + this.Draws * 0.5 + this.Losses * 0.0;
        }
    }

    public int Compareto(object obj) { ... }

    public override bool Equals(object obj) { ... }
    public override int GetHashCode() { ... }
    public static bool operator ==(RRWinRecord lhs,RRWinRecord rhs) { ... }
    public static bool operator !=(RRWinRecord lhs,RRWinRecord rhs) { ... }
    public static bool operator >(RRWinRecord lhs,RRWinRecord rhs) { ... }
    public static bool operator <(RRWinRecord lhs,RRWinRecord rhs) { ... }
    public static bool operator >=(RRWinRecord lhs,RRWinRecord rhs) { ... }
    public static bool operator <=(RRWinRecord lhs,RRWinRecord rhs) { ... }
}

...

    int r = 1,lastRank = 1;
    RRWinRecord lastRecord = null;

    var ranks = from team in records.Keys
                let teamRecord = records[team]
                orderby teamRecord descending
                select new RRRank() { Team = team,Rank = r++,Record = teamRecord };

    foreach (var rank in ranks)
    {
        if (rank.Record != null && lastRecord == rank.Record)
        {
            rank.Rank = lastRank;
        }

        lastRecord = rank.Record;
        lastRank = rank.Rank;

        string scoreDescription = String.Format("{0}-{1}-{2}",rank.Record.Wins,rank.Record.Losses,rank.Record.Draws);
        yield return new TournamentRanking(rank.Team,rank.Rank,scoreDescription);
    }

    yield break;

解决方法

这应该适用于非密集级别:
static class Program
{

    static IEnumerable<Result> GetResults(Dictionary<TournamentTeam,double> wins,Dictionary<TournamentTeam,double> 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,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;
}

原文地址:https://www.jb51.cc/csharp/96233.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐