C# 按值排序字典-先按字母C降序#

C# 按值排序字典-先按字母C降序#,c#,linq,C#,Linq,假设我们有字典:Dict('a'=>2,'b'=>3,'c'=>2,'d'=>4,'e'=>2) 我使用: var items = from pair in Dict orderby pair.Value descending select pair; 一切正常,输出为: d: 4 b: 3 c: 2 e: 2 a: 2 现在我想按字母顺序对具有相同值的键进行排序,得到: d: 4 b: 3 a: 2 c: 2 e: 2 但是我不知道怎么做。 有什么想法吗?按2个值排序,语法为: var


假设我们有字典:
Dict('a'=>2,'b'=>3,'c'=>2,'d'=>4,'e'=>2)

我使用:

var items = from pair in Dict orderby pair.Value descending select pair;
一切正常,输出为:

d: 4
b: 3
c: 2
e: 2
a: 2
现在我想按字母顺序对具有相同值的键进行排序,得到:

d: 4
b: 3
a: 2
c: 2
e: 2
但是我不知道怎么做。

有什么想法吗?

按2个值排序,语法为:

var items = from pair in Dict 
            orderby pair.Value descending, 
                    pair.Key 
            select pair;

如果我正确理解了您的问题,那么问题就简单到:

var items = Dict.OrderByDescending(r=> r.Value)
                .ThenBy(r=> r.Key);
您需要在多个字段上进行订购使用(或根据您的要求)

var dict=new Dictionary
{
{“a”,2},
{“b”,3},
{“c”,2},
{“d”,4},
{“e”,2}
};
var sorted=dict.OrderByDescending(x=>x.Value),然后是by(x=>x.Key);

@Ulug你应该回答这个问题:你在如何使用LINQ整理数据收集方面做了哪些研究,以及你在研究中发现的哪些方面让你感到困惑?+1,这看起来很完美,不太清楚为什么会被否决。+1,但看起来你也因为某种原因受到了打击。有人心情不好。
var dict = new Dictionary<string, int>
{
   {"a", 2},
   {"b", 3},
   {"c", 2},
   {"d", 4},
   {"e", 2}
};

var sorted = dict.OrderByDescending(x => x.Value).ThenBy(x => x.Key);