C#Windows窗体:使用List <keyvaluepair>创建前10名排行榜

时间:2017-11-18 21:05:26

标签: c# winforms

我正在尝试根据播放器用户名和从文本文件中读取的分数,在C#Windows窗体中创建排名前10的排行榜。

E.g。文本文件中的行:

Denna~21

到目前为止,这是我的代码:

private void scrnLeaderboard_Load(object sender, EventArgs e)
        {
            string[] players = FileMethods.ReadLines();

            // Create List<KeyValuePair> to hold all player usernames and scores
            List<KeyValuePair<int, string>> playerNamesScores = new List<KeyValuePair<int, string>>();

            foreach (var item in players)
            {
                string[] playerDetails = item.Split('~');

                if (playerDetails.Length == 2)
                    // Player's username and score added to List<KeyValuePair> playersNamesScores
                    // Key is score, Value is username
                    playerNamesScores.Add(new KeyValuePair<int, string>(Convert.ToInt32(playerDetails[1]), playerDetails[0].ToString()));
            }

            // Sorting the scores in descending order
            var sortedScores = playerNamesScores.OrderByDescending(x => x).ToList<KeyValuePair<int, string>>();

            // Assigning the appropriate values to each label's text on the leaderboard
            lblPos1.Text = String.Format("{0}: \t{1}", sortedScores[0].Value, sortedScores[0].Key);
            lblPos2.Text = String.Format("{0}: \t{1}", sortedScores[1].Value, sortedScores[1].Key);
            lblPos3.Text = String.Format("{0}: \t{1}", sortedScores[2].Value, sortedScores[2].Key);
            lblPos4.Text = String.Format("{0}: \t{1}", sortedScores[3].Value, sortedScores[3].Key);
            lblPos5.Text = String.Format("{0}: \t{1}", sortedScores[4].Value, sortedScores[4].Key);
            lblPos6.Text = String.Format("{0}: \t{1}", sortedScores[5].Value, sortedScores[5].Key);
            lblPos7.Text = String.Format("{0}: \t{1}", sortedScores[6].Value, sortedScores[6].Key);
            lblPos8.Text = String.Format("{0}: \t{1}", sortedScores[7].Value, sortedScores[7].Key);
            lblPos9.Text = String.Format("{0}: \t{1}", sortedScores[8].Value, sortedScores[8].Key);
            lblPos10.Text = String.Format("{0}: \t{1}", sortedScores[9].Value, sortedScores[9].Key);
        }

FileMethods.ReadLines()就是这样:

string filepath = @"previousPlayers.txt";
string[] players = File.ReadLines(filepath).ToArray();
return players;

每次编译代码时,都会收到此错误, &#39;至少有一个对象必须实现IComparable。&#39;, 在这一行:

var sortedScores = playerNamesScores.OrderByDescending(x => x).ToList<KeyValuePair<int, string>>();

我不确定这意味着什么,或者我的代码是如何运作的。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

OrderByDescending方法按键对项目进行排序,由键选择器返回(此方法的第一个参数)。然后,它使用单个项目的这些键将一个项目与另一个项目进行比较以确定哪些项目首先出现,哪些出现在后面。默认情况下,它使用项目实现的IComparable接口方法进行此比较。但是您将x => x指定为键选择器,它返回KeyValuePair<int, string>类型的对象,但不实现IComparable。因此,没有办法对它们进行排序,你得到了&#34; 至少有一个对象必须实现IComparable &#34;错误。

由于您只想按分数(类型int)和int实施IComparable订购商品,您只需使用返回分数的选择器:

var sortedScores = playerNamesScores.OrderByDescending(x => x.Key).ToList<KeyValuePair<int, string>>();

如果需要更复杂的排序方式(例如,您需要具有相同分数的播放按字母顺序排序),您可以声明实现IComparer<KeyValuePair<int, string>>接口的类并将其作为第二个实例传递OrderByDescending方法的论据。

答案 1 :(得分:0)

你在哪里定义x?

forbiddenValues

在本节中,您通常将要排序的变量命名为,但从此处显示的示例中,“x”似乎无处不在。

您也尝试对KeyValuePair进行排序,而不是尝试对int或字符串进行排序。 KeyValuePair本身不可排序。