C# Ranking of objects, multiple criteria

前端 未结 5 867
日久生厌
日久生厌 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 12:54

    Assuming you have a List structure where the Result object has the following parameters...

    Pesron     - string
    Rank       - int
    Wins       - double
    TotalScore - int
    

    You could write a custom comparer, and then pass that to List.Sort(Comparison comparison)

    Alternative, you could just make your Result object implement IComparable and stick this in your class.

            #region IComparable Members
    
            public int CompareTo(Result obj)
            {
                if (this.Rank.CompareTo(obj.Rank) != 0)
                    return this.Rank.CompareTo(obj.Rank);
    
                if (this.Wins.CompareTo(obj.Wins) != 0)
                    return (this.Wins.CompareTo(obj.Wins);
    
                return (this.TotalScore.CompareTo(obj.TotalScore) ;
    
            }
    
            #endregion
    

    Then you can just call List.Sort();

提交回复
热议问题