sort 2 dictionaries linq c#

I have 2 nested dictionaries and I want to sort it with 2 criteria. First to orderby by Value.Count and then to order inside dictionary by Value(decimal). I fail with the second sort, because b.Value.Values get Collection which is not sortable. How can solve it ?

string path = @"D:\";
        string saveReportPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    Dictionary<string, Dictionary<string, long>> folderContent = new Dictionary<string, Dictionary<string, long>>();

    string[] files = Directory.GetFiles(path);

    foreach (var file in files)
    {
        string key = Regex.Match(file, @"\.[a-z\d]+$").Value;
        if (!folderContent.ContainsKey(key))
        {
            folderContent.Add(key, new Dictionary<string, long>());
        }
        FileInfo currentFileInfo = new FileInfo(file);
        folderContent[key].Add(currentFileInfo.Name, currentFileInfo.Length);
    }
Dictionary<string, Dictionary<string, decimal>> folderContent =
   new Dictionary<string, Dictionary<string, decimal>>();

var orderedDict = folderContent
    .OrderByDescending(a => a.Value.Count)
    .ThenBy(b => b.Value.Values);

Simple input

D:\

Output

    .jpg
--FileName.jpg - 1237128kb
--FileName.jpg - 12371282323kb
--FileName.jpg - 1237122323232238kb
.zip
Class.cs - 123kb
and so on..........

Answers


But the second dictionary has multiple values. You could use sort on first or last item in the values or min/max values or even the sum.

var orderedDict = folderContent
.OrderByDescending(a => a.Value.Count)
.ThenBy(b => b.Value.Values.Sum());

Need Your Help

vim/perl insert line when matching regex from cli

perl vim

I have a large config file where I need to search for a variable. When I find it, I need to replace the entire line containing that variable with a specific line (say 123456).

PHP custom class and function autocompletion in TextMate project

php autocomplete textmate textmatebundles

Is there a bundle which completes my custom classes and functions in a TextMate project?