C#如何按值对嵌套字典进行排序 [英] C# How to sort nested dictionary by value

查看:83
本文介绍了C#如何按值对嵌套字典进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我要在这里完成的工作是,我有一本字典,其数据如下所示:

Okay, what I am trying to accomplish here is I have a dictionary with data that looks like this:

未排序:

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

我需要按人口最多的国家/地区对字典进行排序,然后按人口最多的城市对字典进行排序,所以它看起来像这样:

I need to sort the dictionaries by Country with largest population and then every city by largest population so it looks something like this:

排序:

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>>();

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

I already sorted the countries using this line of code:

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

但是我正在努力寻找一种解决方案,以对嵌套字典与国家/地区进行排序.

But I am struggling to find a solution for sorting the nested dictionary with the countries.

我正在尝试使用单个linq语句来执行此操作,但是如果不可能,那么foreach解决方案也将不胜感激.谢谢!

I am trying to do this with a single linq statement, but if its not possible a foreach solution would also be appreciated. Thanks!

@J_L 的解决方案正是我想要的:

@J_L's solution was exactly what I was looking for:

 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 的解决方案也使其起作用:

@Lucifer's solution also made it work:

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)));

我了解到,有些人告诉我使用其他方法,因此请尝试按照某些人的建议使用列表.我还学到了一些新东西,因此感谢大家的帮助.

I understand that some of you are telling me to use a different approach, so will try using lists as some of you suggested. I also learned some new things, so thanks to everyone for the help.

推荐答案

我认为这是您要寻找的:

I think this is what you are looking for:

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);
}

输出:

美国迈阿密-5纽约-4法国巴黎-7西班牙马德里-3巴塞罗那-1

这篇关于C#如何按值对嵌套字典进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆