【问题标题】:How to sort by descending nested Dictionary in C# using LINQ如何使用 LINQ 在 C# 中对嵌套字典进行降序排序
【发布时间】:2017-04-21 18:05:36
【问题描述】:

我需要一些帮助来按嵌套字典的降序排序,这对我来说很难,因为我不是那么先进,我一直在搜索很多网站但没有成功。如果有人可以帮我解决这个问题,我将不胜感激。所以这里是代码

Dictionary<string, Dictionary<string, int>> champLeague = new Dictionary<string, Dictionary<string, int>>();

例如当我添加 -

巴塞罗那,阿森纳,1

曼联,利物浦,2

曼城,斯托克城,3

我想打印出按第二个字典的值降序排序的字典,如下所示:

var orderedDic = champLeague.OrderByDescending(x => x.Value.Values).ThenBy(x => x.Value.Keys)

然后试试foreach(var kvp in orderedDic){Console.WriteLine(kvp.Key)} 它向我抛出了一个异常:“未处理的异常至少必须实现一个对象 IComparable”

我想看起来像这样:

曼城

曼联

巴塞罗那

【问题讨论】:

  • 字典用于查找,但是您可以像使用任何其他 IEnumerable 一样使用它。但是您如何尝试输出您的数据?你的例子不够通用。如果将相同的值添加到两个字典中,它们应该如何显示?例如 Barcelona, Arsenal, 1 Man United, Liverpool, 1 您是要连续列出这些,还是应该合并所有字典及其值?
  • 例如当我添加 ... - 我们都是这里的程序员,你可以发布一些代码而不是列出一些不清楚它去向的数据。
  • 从您的问题陈述中,我可以理解您想根据进球数来整理比赛。对于每对团队,都会有多个目标。我说的对吗?
  • 听起来你正在使用字典来避免创建包含城市、团队和排名的类
  • 好的,如果你想看一下,这里是带有 cmets 的完整代码 - pastebin.com/rivgGsWE

标签: c# .net linq dictionary


【解决方案1】:
        foreach (var firstTeam in dictionary)
        {
            Console.WriteLine($"{firstTeam.Key}");
            foreach (var secondTeam in firstTeam.Value.OrderByDescending(x => x.Value))
            {
                Console.WriteLine($"#  {secondTeam .Key} -> {secondTeam .Value}");
            }
        }

【讨论】:

  • 如果您只想在打印结果时使用排序,您可以使用此代码
【解决方案2】:

据我了解,您希望根据进球数按降序对比赛进行排序。对于这个特殊问题,我认为不建议使用字典。您可以改用元组。当一支球队有不止一场比赛时,他们将为您省去麻烦。 这是代码。

using System;
using System.Linq;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {
        var tuple1 =
            new Tuple<string, string, int>("Man City", "Stoke City", 3);
        var tuple2 =
            new Tuple<string, string, int>("Man Unted", "Liverpool", 2);
        var tuple3 =
            new Tuple<string, string, int>("Barcelona", "Arsenal", 1);

        var championsLeague = new List<Tuple<string, string, int>>();
        championsLeague.Add(tuple1);
        championsLeague.Add(tuple2);
        championsLeague.Add(tuple3);

        //Item3 is the third item from left that mentioned in the definition of the Tuple. ie. Number of goals.
        var lst = championsLeague.OrderByDescending(x => x.Item3)
                           .Select(x=> x.Item1); // Item1 is the first item mentioned in the tuple definition.

        lst.ToList().ForEach(Console.WriteLine);


    }
}

【讨论】:

  • “我想知道Item1Item3 是什么意思”...每个维护者都说...永远。
【解决方案3】:

你应该试试

var allMatches = new List<KeyValuePair<KeyValuePair<string, string>, int>>();
foreach(var left in champLeage.Keys)
{
    foreach(var right in left){
        allMatches.Add(new KeyValuePair(new KeyValuePair<left, right>(left, right.Key), right.Value);
    }
} 

foreach(var match in allMatches.OrderByDescending(x => x.Value)){
    ConsoleWriteLine("{0} - {1} : {2}", match.Key.Key, match.Key.Value, match.Value);
}

这既不高效也不“漂亮”。你应该使用类。一个有 2 个团队和一个 Result 或类似的东西的 Match 类

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 2016-01-17
    • 2020-04-29
    • 2019-09-09
    • 1970-01-01
    • 2018-11-23
    • 2016-05-30
    • 1970-01-01
    相关资源
    最近更新 更多