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);
Anonymous Duck
  • 2,942
  • 1
  • 12
  • 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
  • 180,369
  • 20
  • 160
  • 215
0

This might do the trick for you

logInfo.OrderBy(x=>x.Value.Size);
Mohit S
  • 13,723
  • 6
  • 34
  • 69
0
var sorted = logInfo.OrderBy(pair => pair.Value.Size);
hyankov
  • 4,049
  • 1
  • 29
  • 46