c# - How to sort a Dictionary in ascending and descending order

Dictionary sort order
The Dictionary class represents a collection of keys and values. .Net framework’s Dictionary is located under the System.Collections.Generic namespace. The Dictionary object constructor is Dictionary<TKey,TValue>. The TKey is the data type of the keys in the Dictionary and the TValue is the data type of the values in the Dictionary. We can initialize an empty Dictionary instance and add elements to it using its Add() method.

The following .net c# tutorial code demonstrates how we can sort Dictionary elements. And how .net developers can sort dictionary elements in both ascending and descending order? How .net c# developers can sort dictionary items by their keys or values.

The .net c# developers can sort Dictionary items by their keys in both ascending and descending order and they also can sort Dictionary items by their values both in ascending and descending order. In the following example code, we sort Dictionary elements in descending order by their keys, and we also sort Dictionary elements in ascending order by their values.

The Enumerable OrderBy() method sorts the elements of a sequence in ascending order. We sort the Dictionary elements by their values in ascending order using this OrderBy() method.

The Enumerable OrderByDescending() method sorts the elements of a sequence in descending order. In this example we sort Dictionary items by their keys in descending order using this OrderBYDescending() method.

The Enumerable OrderBy() method and OrderByDescending methods exist in System.Linq namespace.

The Enumerable ToDictionary() method creates a Dictionary<TKey,TValue> object from an IEnumerable<T>.
dictionary-sort-order.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>    
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        //initialize a dictionary with values.
        Dictionary<int, string> birds = new Dictionary<int, string>() {
            {1,"Cuban Amazon"},
            {2,"Pheasant Cuckoo"},
            {3,"African Emerald Cuckoo"},
            {4,"Regent Parrot"},
            {5,"Guira Cuckoo"}
        };

        Label1.Text = "birds dictionary keys and values..........";
        foreach (KeyValuePair<int, string> pair in birds)
        {
            Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
        }

        //descending sorted dictionary by keys.
        birds = birds.OrderByDescending(x => x.Key).ToDictionary(x => x.Key, x => x.Value);

        Label1.Text += "<br /><br />descending sorted dictionary by keys..........";
        foreach (KeyValuePair<int, string> pair in birds)
        {
            Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
        }

        //ascending sorted dictionary by values.
        birds = birds.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);

        Label1.Text += "<br /><br />ascending sorted dictionary by values..........";
        foreach (KeyValuePair<int, string> pair in birds)
        {
            Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
        }
    }    
</script>    

<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - dictionary sort order</title>    
</head>    
<body>    
    <form id="form1" runat="server">    
    <div>    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - dictionary sort order
        </h2>    
        <hr width="550" align="left" color="Gainsboro" />    
        <asp:Label     
            ID="Label1"     
            runat="server"    
            Font-Size="Large"  
            >    
        </asp:Label>    
        <br /><br />  
        <asp:Button     
            ID="Button1"     
            runat="server"     
            Text="dictionary sort order"    
            OnClick="Button1_Click"  
            Height="40"    
            Font-Bold="true"    
            />    
    </div>    
    </form>    
</body>    
</html>