If you are working with Dictionary, you may want to sort dictionary by value, so in this article, I have mentioned how you can easily sort C# Dictionary by Value using Linq.

In one line, you can simply use 'myDictionary.OrderBy(x => x.Value);' to sort it, here is the complete C# Console application example.

using System;
using System.Collections.Generic;
using System.Linq;

namespace DictionarySorting
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, int> myDict = new Dictionary<string, int>();
            myDict.Add("Anand ", 1);
            myDict.Add("Amar", 4);
            myDict.Add("Vijay ", 2);
            myDict.Add("Vikram ", 3);

            Console.WriteLine("After Sorting");

            //sorting dictionary using value
            //using Linq
            var sortedDict = myDict.OrderBy(x => x.Value);

            //print sorted dictionary
            foreach(var keyPair in sortedDict)
            {
                Console.WriteLine(keyPair.Key + " "+ keyPair.Value);
            }
        }
    }
}

Output:

After Sorting
Anand  1
Vijay  2
Vikram  3
Amar 4

csharp-dictionary-sort-by-value

As you can see in the above example, we are simply using OrderBy C# Linq method to sort Dictionary by value.

You may also like to read:

Read CSV file data in C#

Get Date Difference in C#

C# TimeSpan (With Example)

Get String from Array in C#

C# Comments with example (Single or Multi-line)

Switch case multiple conditions in C#

How to define C# multiline string literal?

Encrypt and Decrypt files in C#

Int to Enum or Enum to Int in C#

Generate Random alphanumeric strings in C#

Generate Random Number in C#

Various star pattern program in c#

Get IP Address using C# (Local and Public IP Example)