SortedDictionary作为高分榜,向下移动高分1槽

时间:2016-08-02 20:49:57

标签: c#

我有一个基于SortedDictionary的HighScore系统,如下所示:

SortedDictionary<string, int> highscoDictionary = new SortedDictionary<string, int>

字符串将是Highscore的关键,例如HighScore 1,它将指向该值。

如果我有:

  

高分1分:5500

     

高分2分:4500

     

高分3分:3500

     

高分4分:2500

     

高分6分:0

     

高分7分:0

     

高分8分:0

     

高分9分:0

我可以像这样轻松取代Highscore 2:

foreach (KeyValuePair<string, int> score in highscoDictionary)
{
     if (score.Value < tempScore)
     {
            highscoDictionary[score.Key] = tempScore;
            break;
     }
 }

但是如何将其他高分1槽向下移动,以便HighScore 2替换为3,而3将替换4等等。

1 个答案:

答案 0 :(得分:1)

默认情况下,排序的集合按键从最低到最高排序,因此您可能需要

SortedList<int, string> highScores = new SortedList<int, string>();

highScores.Add(3500, "Player 1");
highScores.Add(2500, "Player 2");

这样,当您添加分数时,分数将自动排序,但要从最高到最低显示分数,您必须从最后一个元素开始。

相关问题