Sort a Dictionary by Value in C#

The following code snippet sorts a Dictionary by values. 

The code first creates a dictionary and then uses OrderBy method to sort the items. 

public void SortDictionary()
{

    // Create a dictionary with string key and Int16 value pair
    Dictionary<stringInt16> AuthorList = new Dictionary<stringInt16>();
    AuthorList.Add("Mahesh Chand", 35);
    AuthorList.Add("Mike Gold", 25);
    AuthorList.Add("Praveen Kumar", 29);
    AuthorList.Add("Raj Beniwal", 21);
    AuthorList.Add("Dinesh Beniwal", 84); 

    // Sorted by Value

    Console.WriteLine("Sorted by Value");
    Console.WriteLine("=============");
    foreach (KeyValuePair<stringInt16> author in AuthorList.OrderBy(key => key.Value))
    {
        Console.WriteLine("Key: {0}, Value: {1}", author.Key, author.Value);
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all