C# 如何按升序对词典进行排序<;字符串,列表<;字符串>&燃气轮机;基于值长度?

C# 如何按升序对词典进行排序<;字符串,列表<;字符串>&燃气轮机;基于值长度?,c#,C#,这里key是一个字符串,value是一个字符串列表。最长的名单应该排在第一位,最后是最短的 Dictionary<string, List<string>> InputDictionary = {‘A’:[A1, A2, A3, A4], ‘B’:[B1, B2], ‘C’:[C1, C2, C3]} 请回复。您的意思是降序排列 InputDictionary = InputDictionary .OrderByDescending(kv => kv.

这里key是一个字符串,value是一个字符串列表。最长的名单应该排在第一位,最后是最短的

Dictionary<string, List<string>> InputDictionary = {‘A’:[A1, A2, A3, A4], ‘B’:[B1, B2], ‘C’:[C1, C2, C3]}

请回复。

您的意思是降序排列

InputDictionary = InputDictionary
      .OrderByDescending(kv => kv.Value.Count)
      .ToDictionary(kv => kv.Key, kv => kv.Value);

然而,字典本质上是无序的

这应该能帮到你

Dictionary<char, List<string>> input = new Dictionary<char, List<string>>();
input.Add('A', new List<string>() { "A1", "A2", "A3", "A4" });
input.Add('B', new List<string>() { "B1", "B2", "B3", "B4", "B5" });
input.Add('C', new List<string>() { "C1", "C2", "C3" });
input.Add('D', new List<string>() { "D1", "D2" });

input = input.OrderByDescending(x => x.Value.Count).ToDictionary(x => x.Key, x => x.Value);
字典输入=新建字典();
input.Add('A',newlist(){“A1”、“A2”、“A3”、“A4”});
input.Add('B',newlist(){“B1”、“B2”、“B3”、“B4”、“B5”});
Add('C',new List(){“C1”,“C2”,“C3”});
Add('D',new List(){“D1”,“D2”});
input=input.OrderByDescending(x=>x.Value.Count).ToDictionary(x=>x.Key,x=>x.Value);
这真的是C#吗?它看起来几乎像JSON。你是如何输出的?请澄清?
Dictionany
是一个无序的容器。您想要一个
SortedDictionary
还是一个排序键列表?
Dictionary<char, List<string>> input = new Dictionary<char, List<string>>();
input.Add('A', new List<string>() { "A1", "A2", "A3", "A4" });
input.Add('B', new List<string>() { "B1", "B2", "B3", "B4", "B5" });
input.Add('C', new List<string>() { "C1", "C2", "C3" });
input.Add('D', new List<string>() { "D1", "D2" });

input = input.OrderByDescending(x => x.Value.Count).ToDictionary(x => x.Key, x => x.Value);