ZetCode

C# sort Dictionary

last modified June 15, 2022

C# sort Dictionary tutorial shows how to sort Dictionary elements in C# language. C# tutorial is a comprehensive tutorial on C# language.

Sorting

In programming, sorting means arranging elements in an ordered sequence. Over the years, several algorithms were developed to perform sorting on data, including merge sort, quick sort, selection sort, or bubble sort.

The opposite of sorting, rearranging a sequence of elements in a random or meaningless order, is called shuffling. We can sort data alphabetically or numerically. The sort key specifies the criteria used to perform the sort.

C# has built-in methods for efficient sorting of data.

C# Sort Dictionary by value

The following example, we sort a small dictionary.

Program.cs
var users = new Dictionary<string, int>()
{
    { "John", 41 },
    { "Jane", 38 },
    { "Lucy", 29 },
    { "Paul", 24 }
};

var sorted = users.OrderBy(user => user.Value);

foreach (var user in sorted)
{
    Console.WriteLine($"{user.Key} is {user.Value} years old");
}

Console.WriteLine("----------------------------");

var sorted2 = users.OrderByDescending(user => user.Value);

foreach (var user in sorted2)
{
    Console.WriteLine($"{user.Key} is {user.Value} years old");
}

With OrderBy and OrderByDescending methods, we sort the dictionary by its values in ascending and descending order.

$ dotnet run
Paul is 24 years old
Lucy is 29 years old
Jane is 38 years old
John is 41 years old
----------------------------
John is 41 years old
Jane is 38 years old
Lucy is 29 years old
Paul is 24 years old

In the next example, we use the query expression syntax.

Program.cs
var users = new Dictionary<string, int>()
{
    { "John", 41 },
    { "Jane", 38 },
    { "Lucy", 29 },
    { "Paul", 24 }
};

var sorted = from pair in users
    orderby pair.Value
    select pair;

foreach (var user in sorted)
{
    Console.WriteLine($"{user.Key} is {user.Value} years old");
}

Console.WriteLine("------------------------");

var sorted2 = from pair in users
    orderby pair.Value descending
    select pair;

foreach (var user in sorted2)
{
    Console.WriteLine($"{user.Key} is {user.Value} years old");
}

The example sorts elements of a dictionary by its values in ascending and descending order using LINQ query expression syntax.

C# Sort Dictionary by keys

Next, we sort a dictionary by keys.

Program.cs
var users = new Dictionary<string, int>()
{
    { "John", 41 },
    { "Jane", 38 },
    { "Lucy", 29 },
    { "Paul", 24 }
};

var sorted = users.OrderBy(user => user.Key);

foreach (var user in sorted)
{
    Console.WriteLine($"{user.Key} is {user.Value} years old");
}

Console.WriteLine("----------------------------");

var sorted2 = users.OrderByDescending(user => user.Key);

foreach (var user in sorted2)
{
    Console.WriteLine($"{user.Key} is {user.Value} years old");
}

The example sorts dictionary elements by its keys in ascending and descending order using OrderBy and OrderByDescending methods.

$ dotnet run
Jane is 38 years old
John is 41 years old
Lucy is 29 years old
Paul is 24 years old
----------------------------
Paul is 24 years old
Lucy is 29 years old
John is 41 years old
Jane is 38 years old

C# SortedDictionary

SortedDictionary represents a collection of key/value pairs that are sorted on the key.

Program.cs
var sortedUsers = new SortedDictionary<string, int>()
{
    { "John", 41 },
    { "Jane", 38 },
    { "Lucy", 29 },
    { "Paul", 24 }
};

foreach (var user in sortedUsers)
{
    Console.WriteLine($"{user.Key} is {user.Value} years old");
}

The example demonstrates the usage of the SortedDictionary.

In this article we sorted Dictionary elements in C# language.

List all C# tutorials.