【问题标题】:C# How to sort nested dictionary by valueC#如何按值对嵌套字典进行排序
【发布时间】:2018-11-23 00:44:07
【问题描述】:

好的,我想在这里完成的是我有一个字典,其中包含如下所示的数据:

未分类:

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

我知道你们中的一些人告诉我要使用不同的方法,所以我会按照你们中的一些人的建议尝试使用列表。我也学到了一些新东西,所以感谢大家的帮助。

【问题讨论】:

  • 字典中的项目顺序没有意义。访问是通过密钥完成的。如果您希望对其进行排序,可以考虑使用不同的数据结构。例如,不要调用.ToDictionary,而是将其存储在一个元组列表中。也看看How do you sort a dictionary by value?
  • 你使用了错误的集合字典没有排序你想要一个 SortedList
  • 我只想在这里上课。

标签: c# sorting dictionary


【解决方案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

【讨论】:

    【解决方案2】:

    这是您需要的工作示例:-

    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
    

    脚注:- 你真的需要改变你的数据结构,因为如果数据量很大,那么处理起来真的很困难

    【讨论】:

    • 这行得通,因为现有的Dictionary 按照您添加它们的顺序返回条目(除非您删除条目)。请注意,这只是一个实现细节——微软不保证这种行为会继续下去。因此,不建议这样做。
    • 但我不明白拒绝投票的原因这个答案解决了 Op 的要求。
    • 代码有契约和实现。合同上写着“这是我的承诺——相信这个”。实现说“这就是我现在的工作方式”。当您应该依赖合同时,您依赖的是实施。文档清楚地说明了 (msdn.microsoft.com/en-us/library/…) - For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair&lt;TKey, TValue&gt; structure representing a value and its key. The order in which the items are returned is undefined.。订单未定义 - 您不能依赖它。
    • @mjwills 现在我明白你想表达什么了,这里的 Op 正在使用字典,但正如你们建议的那样,他应该使用 SortedListt 来实现他想要实现的任何目标。我将使用排序列表/字典解决方案编辑答案
    • 我不相信SortedList 也可以,但试试看吧。
    猜你喜欢
    • 2020-11-11
    • 2014-05-01
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多