C#如何按值对嵌套字典进行排序

时间:2018-06-13 12:20:15

标签: c# sorting dictionary

好的,我在这里要完成的是我有一个字典,其数据看起来像这样:

未排序:

US ( total population: 9 )
-New York - 4
-Miami - 5
Spain ( total population: 4 )
-Madrid - 3
-Barcelona - 1
France ( total population: 7 )
-Paris - 7

我需要按人口最多的国家排序词典,然后按人口最多的每个城市排序,这样看起来像这样:

排序:

US ( total population: 9 )
-Miami - 5
-New York - 4
France ( total population: 7 )
-Paris - 7
Spain ( total population: 4 )
-Madrid - 3
-Barcelona - 1

我有:

var worldPopulation = new Dictionary<string, Dictionary<string, long>>();

我已经使用以下代码行对国家/地区进行了排序:

worldPopulation = worldPopulation.OrderByDescending(x => x.Value.Values.Sum()).ToDictionary(x => x.Key, x => x.Value);

但我正在努力寻找一种解决方案,用于对国家/地区的嵌套字典进行排序。

我试图用一个linq语句来做这个,但如果不可能的foreach解决方案也会受到赞赏。谢谢!

编辑:

@J_L 的解决方案正是我所寻求的:

 worldPopulation = worldPopulation.OrderByDescending(x => x.Value.Values.Sum()).ToDictionary(x => x.Key, x => x.Value.OrderByDescending(y => y.Value).ToDictionary(y => y.Key, y => y.Value));

@Lucifer 的解决方案也让它发挥作用:

var worldPopulationSorted = new Dictionary<string, Dictionary<string, long>>();

worldPopulation.OrderByDescending(dic => dic.Value.Values.Sum()).ToList().ForEach(x => worldPopulationSorted.Add(x.Key, x.Value.OrderByDescending(y => y.Value).ToDictionary(y => y.Key, y => y.Value)));

据我所知,有些人告诉我使用不同的方法,因此会尝试使用列表,就像你们建议的那样。我也学到了一些新东西,感谢大家的帮助。

2 个答案:

答案 0 :(得分:1)

我认为这就是你要找的东西:

var worldPopulation = new Dictionary<string, Dictionary<string, long>>()
{
    {"US", new Dictionary<string, long>()
    {
        { "New York", 4 },
        { "Miami", 5 }
    }},
    {"Spain", new Dictionary<string, long>()
    {
        { "Madrid", 3 },
        { "Barcelona", 1 },
    }},
    {"France", new Dictionary<string, long>()
    {
        { "Paris", 7 },
    }},
};


worldPopulation = worldPopulation.OrderByDescending(x => x.Value.Values.Sum()).
    ToDictionary(x => x.Key, 
    x => x.Value.OrderByDescending(y => y.Value).ToDictionary(y => y.Key, y => y.Value));

foreach (var country in worldPopulation)
{
    Console.WriteLine(country.Key);
    foreach (var city in country.Value)
        Console.WriteLine("   " + city.Key + " - " + city.Value);
}

<强>输出:

US Miami - 5 New York - 4 France Paris - 7 Spain Madrid - 3 Barcelona - 1

答案 1 :(得分:0)

以下是您需要的工作示例: -

var worldPopulationSorted = new Dictionary<string, Dictionary<string, long>>();

worldPopulation.OrderByDescending(dic => dic.Value.Values.Sum()).ToList().ForEach(x => worldPopulationSorted.Add(x.Key, x.Value.OrderByDescending(y => y.Value).ToDictionary(y => y.Key, y => y.Value)));

我使用的示例: -

var worldPopulation = new Dictionary<string, Dictionary<string, long>>();

Dictionary<string, long> sample = new Dictionary<string, long>();
sample.Add("1", 5);
sample.Add("2", 6);
sample.Add("3", 7);

worldPopulation.Add("first", sample);

sample = new Dictionary<string,long>();
sample.Add("3", 9);
sample.Add("2", 9);
sample.Add("1", 9);

worldPopulation.Add("second", sample);

var worldPopulationSorted = new Dictionary<string, Dictionary<string, long>>();

worldPopulation.OrderByDescending(dic => dic.Value.Values.Sum()).ToList().ForEach(x => worldPopulationSorted.Add(x.Key, x.Value.OrderByDescending(y => y.Value).ToDictionary(y => y.Key, y => y.Value)));

输出: -

second
   3 - 9
   2 - 9
   1 - 9
first
   3 - 7
   2 - 6
   1 - 5
  

FootNote: - 如果数据量很大,你真的需要改变数据的结构,那么它真的很难处理