How to Sort dictionary by value in C#?

The Dictionary contains the KeyValuePair and the below is a sample code snippet that demonstrates the sorting of the dictionary entries by value in C#.

How to Sort dictionary by value in C#?

using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

namespace AbundantCode

{

internal class Program

{

//Using Or Operator in the Where Clause of LINQ in C#

private static void Main(string[] args)

{

Dictionary<int, string> Employees = new Dictionary<int, string>();

Employees.Add(1, "Martin");

Employees.Add(3, "Scott");

Employees.Add(10, "Mike");

List<KeyValuePair<int, string>> EmployeeSorted = Employees.ToList();

EmployeeSorted.Sort((FirstValue, SecondValue) =>

{

return FirstValue.Value.CompareTo(SecondValue.Value);

}

);

foreach (var item in EmployeeSorted)

{

Console.WriteLine(item.Value);

}

Console.ReadKey();

}

}

}
%d