---
title: "How to sort the generic SortedList in descending order?"  
description: "How to sort the generic SortedList in descending order?"  
author: "Sandra Emily"  
published: 2024-06-13  
updated: 2024-06-13  
canonical: https://www.mindstick.com/forum/160740/how-to-sort-the-generic-sortedlist-in-descending-order  
category: "c#"  
tags: ["c#", "collection", "sortedlist"]  
reading_time: 2 minutes  

---

# How to sort the generic SortedList in descending order?

How to sort the generic SortedList in [descending order](https://answers.mindstick.com/qa/36839/how-do-i-sort-a-series-of-number-ascending-or-descending-order-in-assembly-language-8086)?

## Replies

### Reply by Ashutosh Patel

### 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](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience). 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#,

```cs
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-

```plaintext
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.

**Also, Read:** [How to write file using StreamWriter in C#?](https://www.mindstick.com/forum/160741/how-to-write-file-using-streamwriter-in-c-sharp)


---

Original Source: https://www.mindstick.com/forum/160740/how-to-sort-the-generic-sortedlist-in-descending-order

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
