且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

对列表进行排序并确定索引

更新时间:2023-12-03 17:14:52

使用LINQ:

using System.Linq;

var ranked = playerScores.Select((score, index) => 
                                 new {Player=index+1, Score=score})
                         .OrderByDescending(pair => pair.Score)
                         .ToList();

然后显示获胜者,例如:

And then display the winner, for example:

Console.WriteLine(String.Format("Winner: Player {0} with score {1}", 
                  ranked[0].Player, ranked[0].Score));