Best Ways to Sort Dictionary by Keys and Values in ascending, descending order in C# Examples
Dictionary is a data structure in C#, that stores key and value pairs similar to HashTables in Java.
data stored in this are not sorted, but retrieving is a constant lookup.
This tutorial explains multiple ways to sort a dictionary of keys and values by ascending and descending
- Sort the Dictionary by Key in ascending and descending
- How to Sort Dictionary by Values in ascending and descending.
The dictionary has no sort of method. We have to follow approaches.
Sort Dictionary by Keys and values using Linq OrderBy Method
One way using the OrderBy LINQ method
OrderBy
is a LINQ extension method that returns IEnumerable<KeyValuePair<,>>
. It iterates the Dictionary and applies orderBy with a given conditional expression.
Need to import
to work OrderBy
method
using System.Linq;
using System.Collections.Generic;
OrderByDescending
is another method in Linq to sort keys and values in asc
and desc
order.
OrderBy
can be used in the following ways
// To sort by keys in ascending order
dictionary.OrderBy(x => x.Key);
// To sort by values in ascending order
dictionary.OrderBy(x => x.Value);
// To sort by keys in descending order
dictionary.OrderByDescending(x => x.Key);
// To sort by values in descending order
dictionary.OrderByDescending(x => x.Value);
Here is an example
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var employees = new Dictionary<int, string>
{
{ 1, "one" },
{ 4, "four" },
{ 2, "two" },
{ 3, "three" },
};
// 👉 Dictionary Iterate Print - normal order
Console.WriteLine("👇 Print Dictionary in normal order 👇");
foreach (var item in employees)
{
Console.WriteLine(item.Key + ": " + item.Value);
}
Console.WriteLine("👇 Sort Keys in ascending order 👇");
// 👉 Dictionary Iterate Print - value ascending order
foreach (var key in employees.Keys.OrderBy(item => item))
{
Console.WriteLine("{0}: {1}", key, employees[key]);
}
Console.WriteLine("👇 Sort Values in ascending order 👇");
// 👉 Dictionary Sort by value in ascending order
foreach (var key in employees.Keys.OrderBy(item => item))
{
Console.WriteLine("{0}: {1}", key, employees[key]);
}
}
}
Output:
👇 Print Dictionary in normal order 👇
1: one
4: four
2: two
3: three
👇 Sort Keys in ascending order 👇
1: one
2: two
3: three
4: four
👇 Sort Values in ascending order 👇
1: one
2: two
3: three
4: four
Sort Dictionary by Keys and values using SortedDictionary
Dictionary
has no sorting mechanism by default, SortedDictionary
is another class we can use for this purpose
SortedDictionary
stores the keys and values in key in ascending order.
You can do keys sort in descending order using the Reverse
method
The SortedDictionary
object is created like a normal dictionary with inline syntax using a new operator.
Here is an example
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var employees = new SortedDictionary<int, string>
{
{ 1, "one" },
{ 4, "four" },
{ 2, "two" },
{ 3, "three" },
};
// 👉 SortedDictionary Iterate Print - normal order keys ascending
Console.WriteLine(" 👇 SortedDictionary keys ascending Defaul 👇");
foreach (var item in employees)
{
Console.WriteLine(item.Key + ": " + item.Value);
}
Console.WriteLine(" 👇 Sort Keys in descending reverse order 👇");
// 👉 Dictionary Iterate Print - keys descending reverse order
foreach (var item in employees.Reverse())
{
Console.WriteLine(item.Key + ": " + item.Value);
}
}
}
Output:
​```bash 👇 SortedDictionary keys ascending Defaul 👇 1: one 2: two 3: three 4: four 👇 Sort Keys in descending reverse order 👇 4: four 3: three 2: two 1: one