How to sort a list of dictionaries by values of dictionaries in C#?

Sorting a list of dictionaries by their values is a common requirement in C# programming. There are multiple approaches to achieve this, ranging from sorting by keys to sorting by values, and even sorting lists containing multiple dictionaries.

Syntax

Following is the syntax for sorting dictionary entries by values using LINQ −

var sortedByValue = dictionary.OrderBy(x => x.Value);
var sortedByValueDesc = dictionary.OrderByDescending(x => x.Value);

For sorting by keys −

var sortedByKey = dictionary.OrderBy(x => x.Key);

Sorting Dictionary by Keys

The simplest approach is to sort dictionary entries by their keys. This example demonstrates sorting a dictionary alphabetically by key names −

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      var d = new Dictionary<string, int>();
      d.Add("Zack", 0);
      d.Add("Akon", 3);
      d.Add("Jack", 2);
      d.Add("Tom", 1);
      
      // Sort by keys
      var sortedByKeys = d.OrderBy(x => x.Key);
      
      Console.WriteLine("Sorted by Keys:");
      foreach (var kvp in sortedByKeys) {
         Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
      }
   }
}

The output of the above code is −

Sorted by Keys:
Akon: 3
Jack: 2
Tom: 1
Zack: 0

Sorting Dictionary by Values

To sort dictionary entries by their values instead of keys, use OrderBy(x => x.Value) or OrderByDescending(x => x.Value)

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      var scores = new Dictionary<string, int>();
      scores.Add("Alice", 95);
      scores.Add("Bob", 87);
      scores.Add("Charlie", 92);
      scores.Add("Diana", 98);
      
      // Sort by values (ascending)
      var sortedAsc = scores.OrderBy(x => x.Value);
      Console.WriteLine("Sorted by Values (Ascending):");
      foreach (var kvp in sortedAsc) {
         Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
      }
      
      Console.WriteLine("\nSorted by Values (Descending):");
      var sortedDesc = scores.OrderByDescending(x => x.Value);
      foreach (var kvp in sortedDesc) {
         Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
      }
   }
}

The output of the above code is −

Sorted by Values (Ascending):
Bob: 87
Charlie: 92
Alice: 95
Diana: 98

Sorted by Values (Descending):
Diana: 98
Alice: 95
Charlie: 92
Bob: 87

Sorting List of Dictionaries

When working with a list containing multiple dictionaries, you can sort them based on specific key values within each dictionary −

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      var people = new List<Dictionary<string, object>>();
      
      people.Add(new Dictionary<string, object> {
         {"Name", "John"}, {"Age", 25}, {"Salary", 50000}
      });
      
      people.Add(new Dictionary<string, object> {
         {"Name", "Alice"}, {"Age", 30}, {"Salary", 75000}
      });
      
      people.Add(new Dictionary<string, object> {
         {"Name", "Bob"}, {"Age", 22}, {"Salary", 45000}
      });
      
      // Sort by Salary (descending)
      var sortedBySalary = people.OrderByDescending(dict => (int)dict["Salary"]);
      
      Console.WriteLine("Sorted by Salary:");
      foreach (var person in sortedBySalary) {
         Console.WriteLine("Name: {0}, Age: {1}, Salary: {2}", 
            person["Name"], person["Age"], person["Salary"]);
      }
   }
}

The output of the above code is −

Sorted by Salary:
Name: Alice, Age: 30, Salary: 75000
Name: John, Age: 25, Salary: 50000
Name: Bob, Age: 22, Salary: 45000

Comparison of Sorting Methods

Method Use Case Performance
OrderBy(x => x.Key) Sort dictionary by keys O(n log n)
OrderBy(x => x.Value) Sort dictionary by values O(n log n)
Keys.ToList().Sort() Sort keys only, then access values O(n log n)
OrderByDescending() Sort in descending order O(n log n)

Conclusion

Sorting dictionaries in C# can be accomplished using LINQ methods like OrderBy() and OrderByDescending(). You can sort by keys, values, or specific properties within dictionary collections. Choose the appropriate method based on whether you need ascending or descending order and whether you're working with single dictionaries or lists of dictionaries.

Updated on: 2026-03-17T07:04:35+05:30

542 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements