Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Sort KeyValuePairs in C#
The KeyValuePair<TKey, TValue> structure in C# represents a key-value pair that can be stored in collections. When working with lists of KeyValuePairs, you often need to sort them based on either the key or value. C# provides several approaches to accomplish this sorting.
Syntax
Following is the syntax for sorting KeyValuePairs using the Sort() method with a lambda expression −
myList.Sort((x, y) => x.Value.CompareTo(y.Value)); // Sort by Value (ascending) myList.Sort((x, y) => y.Value.CompareTo(x.Value)); // Sort by Value (descending) myList.Sort((x, y) => x.Key.CompareTo(y.Key)); // Sort by Key (ascending)
Following is the syntax for sorting using LINQ OrderBy() method −
var sortedList = myList.OrderBy(x => x.Value).ToList(); // Sort by Value (ascending) var sortedList = myList.OrderByDescending(x => x.Value).ToList(); // Sort by Value (descending)
Using Sort() Method to Sort by Value
The Sort() method with a lambda expression allows you to define custom comparison logic. Here's how to sort KeyValuePairs by their values in descending order −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var myList = new List<KeyValuePair<int, int>>();
// adding elements
myList.Add(new KeyValuePair<int, int>(1, 20));
myList.Add(new KeyValuePair<int, int>(2, 15));
myList.Add(new KeyValuePair<int, int>(3, 35));
myList.Add(new KeyValuePair<int, int>(4, 50));
myList.Add(new KeyValuePair<int, int>(5, 25));
Console.WriteLine("Unsorted List...");
foreach (var val in myList) {
Console.WriteLine(val);
}
// Sort by Value in descending order
myList.Sort((x, y) => y.Value.CompareTo(x.Value));
Console.WriteLine("Sorted List (by Value - Descending)...");
foreach (var val in myList) {
Console.WriteLine(val);
}
}
}
The output of the above code is −
Unsorted List... [1, 20] [2, 15] [3, 35] [4, 50] [5, 25] Sorted List (by Value - Descending)... [4, 50] [3, 35] [5, 25] [1, 20] [2, 15]
Using Sort() Method to Sort by Key
You can also sort KeyValuePairs by their keys. This example demonstrates sorting by key in ascending order −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var myList = new List<KeyValuePair<string, int>>();
// adding elements with string keys
myList.Add(new KeyValuePair<string, int>("Delta", 40));
myList.Add(new KeyValuePair<string, int>("Alpha", 10));
myList.Add(new KeyValuePair<string, int>("Charlie", 30));
myList.Add(new KeyValuePair<string, int>("Beta", 20));
Console.WriteLine("Unsorted List...");
foreach (var val in myList) {
Console.WriteLine(val);
}
// Sort by Key in ascending order
myList.Sort((x, y) => x.Key.CompareTo(y.Key));
Console.WriteLine("Sorted List (by Key - Ascending)...");
foreach (var val in myList) {
Console.WriteLine(val);
}
}
}
The output of the above code is −
Unsorted List... [Delta, 40] [Alpha, 10] [Charlie, 30] [Beta, 20] Sorted List (by Key - Ascending)... [Alpha, 10] [Beta, 20] [Charlie, 30] [Delta, 40]
Using LINQ OrderBy() Method
LINQ provides a more functional approach to sorting with OrderBy() and OrderByDescending() methods −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
var myList = new List<KeyValuePair<int, string>>();
// adding elements
myList.Add(new KeyValuePair<int, string>(3, "Apple"));
myList.Add(new KeyValuePair<int, string>(1, "Banana"));
myList.Add(new KeyValuePair<int, string>(4, "Cherry"));
myList.Add(new KeyValuePair<int, string>(2, "Date"));
Console.WriteLine("Original List...");
foreach (var val in myList) {
Console.WriteLine(val);
}
// Sort by Value using LINQ
var sortedByValue = myList.OrderBy(x => x.Value).ToList();
Console.WriteLine("Sorted by Value (LINQ)...");
foreach (var val in sortedByValue) {
Console.WriteLine(val);
}
// Sort by Key using LINQ
var sortedByKey = myList.OrderByDescending(x => x.Key).ToList();
Console.WriteLine("Sorted by Key Descending (LINQ)...");
foreach (var val in sortedByKey) {
Console.WriteLine(val);
}
}
}
The output of the above code is −
Original List... [3, Apple] [1, Banana] [4, Cherry] [2, Date] Sorted by Value (LINQ)... [3, Apple] [1, Banana] [4, Cherry] [2, Date] Sorted by Key Descending (LINQ)... [4, Cherry] [3, Apple] [2, Date] [1, Banana]
Comparison of Sorting Methods
| Method | In-Place Sorting | Returns New Collection | Performance |
|---|---|---|---|
| List.Sort() | Yes | No | Better for large collections |
| LINQ OrderBy() | No | Yes | More readable, functional style |
Conclusion
Sorting KeyValuePairs in C# can be accomplished using either the Sort() method with lambda expressions for in-place sorting, or LINQ's OrderBy() methods for creating new sorted collections. Choose Sort() for performance-critical scenarios and OrderBy() for more readable, functional programming approaches.
