C#对象排名,多重条件

前端之家收集整理的这篇文章主要介绍了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.f2er.com/csharp/96233.html

猜你在找的C#相关文章