简体   繁体   中英

How to sort by descending nested Dictionary in C# using LINQ

I need some help to sort by descending nested Dictionary which is quite hard for me because I'm not so advanced , I've been searching many sites but with no success. If someone can give me a hand with this I'll be grateful. So here is the code

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

For example when I add -

Barcelona, Arsenal, 1

Man Unted, Liverpool, 2

Man City, Stoke City, 3

And I want to print out the dictionary ordered by descending by the second dictionary's value like this :

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

And try foreach(var kvp in orderedDic){Console.WriteLine(kvp.Key)} It throws me an exception : "unhandled exception at least one object must be implemented IComparable"

I want to look like this :

Man City

Man United

Barcelona

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

You should try

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

This is not efficient or "pretty". You should use classes. A Match class that have 2 teams and a Result or something like that

By what I understand, you want to sort the matches in descending order with respect to the number of goals. For this particular problem, I think usage of dictionary is not recommended. You can use tuples instead. They will save you the hassle when a team has more than one matches. Here is the code.

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


    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM