The SortedList<TKey, TValue> class in C# does not have a built-in method to sort its elements directly in
descending order. However, you can do this by creating a new list or collection and sorting the items from the original SortedList in order with it.
Example-
Here a basic example to sort the SortedList into descending order in c#,
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Creating a SortedList
SortedList<int, string> sortedList = new SortedList<int, string>();
// Add some items to the SortedList
sortedList.Add(3, "Three");
sortedList.Add(1, "One");
sortedList.Add(2, "Two");
// Sort the items in descending order
List<KeyValuePair<int, string>> descendingList = new List<KeyValuePair<int, string>>(sortedList);
descendingList.Sort((x, y) => y.Key.CompareTo(x.Key));
// Print the sorted items
Console.WriteLine("Descending Order");
foreach (var item in descendingList)
{
Console.WriteLine("Key: {0}, Values: {1}", item.Key, item.Value);
}
// also sort the items in ascending order
List<KeyValuePair<int, string>> ascendingList = new List<KeyValuePair<int, string>>(sortedList);
ascendingList.Sort((x, y) => x.Key.CompareTo(y.Key));
// Print the ascending order items
Console.WriteLine("\nAscending Order");
foreach (var item in ascendingList)
{
Console.WriteLine("Key: {0}, Values: {1}",item.Key, item.Value);
}
Console.ReadLine();
}
}
In the above exaple-
We first create a sortedList called SortedList<int, string> and add some items to it.
We then create a List<KeyValuePair<int, string>> named descendingList for Descending order and
ascendingList for Ascending order, and initialize it with the items from the original
SortedList.
We use the Sort method of the List class to sort the items in
descending order based on the key. We provide a custom comparison function using a
lambda expression.
Finally, we print the orders.
Output-
Descending Order
Key: 3, Values: Three
Key: 2, Values: Two
Key: 1, Values: One
Ascending Order
Key: 1, Values: One
Key: 2, Values: Two
Key: 3, Values: Three
This will print the items from the original SortedList in descending order of their key.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sorting SortedList into Descending Order
The
SortedList<TKey, TValue>class in C# does not have a built-in method to sort its elements directly in descending order. However, you can do this by creating a new list or collection and sorting the items from the original SortedList in order with it.Example-
Here a basic example to sort the SortedList into descending order in c#,
In the above exaple-
We first create a sortedList called
SortedList<int, string>and add some items to it.We then create a
List<KeyValuePair<int, string>>named descendingList for Descending order and ascendingList for Ascending order, and initialize it with the items from the original SortedList.We use the
Sortmethod of the List class to sort the items in descending order based on the key. We provide a custom comparison function using a lambda expression.Finally, we print the orders.
Output-
This will print the items from the original SortedList in descending order of their key.
Also, Read: How to write file using StreamWriter in C#?