C# – How to sort a dictionary

Dictionaries are unordered data structures. Key/value pairs aren’t stored in sorted order. When you want the Dictionary in sorted order, there are two simple options:

  • Use OrderBy() to sort the dictionary by key or value.
  • Use a SortedDictionary.

I’ll show both options.

Sort Dictionary with OrderBy()

Use OrderBy() (from System.Linq) to sort the Dictionary by key or value. It returns the Dictionary’s KeyValuePairs in ascending sorted order. I’ll show examples of sorting the following Dictionary:

var dictionary = new Dictionary<string, int>()
{
    ["boy"] = 1,
    ["zebra"] = 3,
    ["apple"] = 10
};
Code language: C# (cs)

Note: I initialized this dictionary with the keys purposely out of order (so it’s obvious when they are sorted).

Sort Dictionary by key

To sort the Dictionary by key, use OrderBy() and specify KeyValuePair.Key in the lambda. Here’s an example of looping through the sorted dictionary key/value pairs:

using System.Linq;

foreach(var kvp in dictionary.OrderBy(kvp => kvp.Key))
{
    Console.WriteLine($"{kvp.Key} appeared {kvp.Value} time(s)");
}
Code language: C# (cs)

This outputs the KeyValuePairs sorted by key:

apple appeared 10 time(s)
boy appeared 1 time(s)
zebra appeared 3 time(s)Code language: plaintext (plaintext)

Sort Dictionary by value

To sort the Dictionary by value, use OrderBy() and specify KeyValuePair.Value in the lambda. Like this:

using System.Linq;

foreach (var kvp in dictionary.OrderBy(kvp => kvp.Value))
{
    Console.WriteLine($"{kvp.Key} appeared {kvp.Value} time(s)");
}
Code language: C# (cs)

This outputs the KeyValuePairs sorted by value:

boy appeared 1 time(s)
zebra appeared 3 time(s)
apple appeared 10 time(s)Code language: plaintext (plaintext)

Sort Dictionary in descending order

Use OrderByDescending() to sort the Dictionary in descending order. Here’s an example:

using System.Linq;

foreach(var kvp in dictionary.OrderByDescending(kvp => kvp.Key))
{
    Console.WriteLine($"{kvp.Key} appeared {kvp.Value} time(s)");
}
Code language: C# (cs)

This outputs the KeyValuePairs in descending order by key:

zebra appeared 3 time(s)
boy appeared 1 time(s)
apple appeared 10 time(s)Code language: plaintext (plaintext)

Use a SortedDictionary

Use a SortedDictionary instead of a regular Dictionary if you want the KeyValuePairs stored in sorted order. Internally, it uses a tree data structure to keep the KeyValuePairs in ascending order (by key) at all times.

Here’s an example of initializing a SortedDictionary and iterating through it with a foreach loop:

var dictionary = new SortedDictionary<string, int>()
{
    ["boy"] = 1,
    ["zebra"] = 3,
    ["apple"] = 10
};

foreach(var kvp in dictionary)
{
    Console.WriteLine($"{kvp.Key} appeared {kvp.Value} time(s)");
}
Code language: C# (cs)

This outputs the KeyValuePairs in sorted order (because that’s how they’re stored):

apple appeared 10 time(s)
boy appeared 1 time(s)
zebra appeared 3 time(s)Code language: plaintext (plaintext)

Leave a Comment