0

我有一个 SortedDictionary,我在其中保存特定玩家的积分和他的名字。我需要做的是按降序对它进行排序,以便我将获胜者放在字典的第一位。我怎样才能做到这一点?

另外,如何在不知道密钥的情况下从列表中获取项目?

SortedDictionary<int, string> dict = new SortedDictionary<int, string>();
dict.Add(player1Pts, playerNames[0]);
dict.Add(player2Pts, playerNames[1]);
dict.Add(player3Pts, playerNames[2]);

谢谢你的帮助!

4

5 回答 5

6

使用以分数作为键的字典并没有什么意义:键必须是唯一的,因此如果两个玩家的分数相同,它将失败。

相反,您应该创建一个Player包含名称和分数的类,并将Player对象存储在List<Player>. 如果您需要按分数对球员进行排序,您可以Sort使用自定义比较器调用列表,或者仅使用 Linq 对结果进行排序:

foreach (Player player in players.OrderByDescending(p => p.Score))
{
    // Do something with player
}
于 2012-12-06T10:19:54.570 回答
1

第一:当您插入另一个值时,排序字典总是会立即排序。

但请注意:使用积分作为 KEY 意味着您不能拥有具有 EQUAL 积分的玩家。

但是如果你想这样做,你可以简单地使用字典的 Last() 方法来获得最高分的玩家:

SortedDictionary<int, String> t = new SortedDictionary<int,string>();
t.Add(5, "a");
t.Add(10, "c");
t.Add(2, "b");
MessageBox.Show((t.Last<KeyValuePair<int,string>>()).Value);

这将导致“c”。

于 2012-12-06T10:28:41.903 回答
0

首先,我认为您应该切换<int, string>玩家姓名的位置应该是关键,而积分是值。

然后您可以按值对其进行排序:

dict.Sort(
    delegate(KeyValuePair<int, double> val1,
    KeyValuePair<int, double> val2)
    {
        return val1.Value.CompareTo(val2.Value);
    }
);

您可以通过 foreach 遍历字典以获取键和值:

 foreach (var pair in asd)
            {
                string some = pair.Key;
                int someValue =  pair.Value;
            }
于 2012-12-06T10:21:27.880 回答
0

我的问题的答案是任何可能需要它的人:)

Player player1 = new Player(playerNames[0], player1Pts);
Player player2 = new Player(playerNames[1], player2Pts);
Player player3 = new Player(playerNames[2], player3Pts);
Player player4 = new Player(playerNames[3], player4Pts);
Player player5 = new Player(playerNames[4], player5Pts);

List<Player> players = new List<Player>();

players.Add(player1);
players.Add(player2);
players.Add(player3);
players.Add(player4);
players.Add(player5);

var sortedPlayers = (from Player play in players
                     orderby play.Points descending
                     select play);

List<Player> sortPlay = (List<Player>)sortedPlayers.ToList();
于 2012-12-06T19:23:45.653 回答
0

虽然这个问题有几个答案,但这些似乎都没有利用 SortedDictionary 结构。如果您希望将 SortedDictionary 设计为最大堆而不是默认的最小堆,我认为最好的解决方案是覆盖 C# 使用的默认比较器。这可以按如下方式完成:

public class DescendingComparer<T>: IComparer<T> where T : IComparable<T>
{
    public int Compare(T x, T y)
    {
        return y.CompareTo(x); //reverses, so compare ascending
                 //this is vs the standard method, which returns x.CompareTo(y)
    }

}

static void Main(string[] args)
{

SortedDictionary<float, string> myDict = new SortedDictionary<float,string>(new DescendingComparer<float>()); //sorts on the key
    string[] name = {"Bill", "Tom", "Susan", "Terry"};
    myDict.Add(.8f, name[0]);
    myDict.Add(.2f, name[1]);
    myDict.Add(.95f, name[2]);
    myDict.Add(.005f, name[4]);

    foreach (KeyValuePair<float, int> j in myDict)
    {
        Console.WriteLine("Key: {0}, Value: {1}",j.Key,j.Value);
    } //now it is stored in increasing order, so accessing largest elements fast
}

另请参见:C#:按降序排序字典

于 2016-07-29T00:34:25.440 回答