0

I have the following three dimensional Dictionary.

class CopyDir
{
    public class MyFileInfo
    {
        public int Num{ get; set; }
        public long Size { get; set; }
    }
    public static Dictionary<string, MyFileInfo> logInfo;

I want to sort the dictionary based on size Field.

logInfo.OrderBy says Error CS1061 'Dictionary' does not contain a definition for 'OrderBy' and no extension method 'OrderBy' accepting a first argument of type 'Dictionary' could be found (are you missing a using directive or an assembly reference?)

ali yami
  • 9
  • 3

4 Answers4

1

You can sort it easily using LINQ extensions. We are sorting the values of the dictionary.

var sorted = logInfo.Values.OrderBy(x=>x.Size);
DarkMakukudo
  • 2,894
  • 1
  • 10
  • 35
1

If you want to represent the dictionary (e.g. print out on the console) with their values ordered, try uing Linq

   var result = logInfo
     .OrderBy(pair => pair.Value.Size)
     .ThenBy(pair => pair.Num); // in case of tie, let's order by Num

Test

   var test = result
     .Select(item => $"{item.Key,6}: Size = {item.Size,6}; Num = {item.Num,6}");

   Console.Write(string.Join(Environment.NewLine, test)); 
Dmitry Bychenko
  • 165,109
  • 17
  • 150
  • 199
0

This might do the trick for you

logInfo.OrderBy(x=>x.Value.Size);
Mohit S
  • 13,378
  • 5
  • 32
  • 66
0
var sorted = logInfo.OrderBy(pair => pair.Value.Size);
hyankov
  • 3,919
  • 1
  • 26
  • 43