MVA Twenty C# Questions Explained - [19 How do I sort a dictionary by value?]
static void Main(string[] args)
{
Dictionary<string, int?> dic = new Dictionary<string, int?>();
dic.Add("Two", 2);
dic.Add("Zero", 0);
dic.Add("Five", null);
dic.Add("Three", 3);
dic.Add("One", 1);
dic.Add("Four", 4);
Dictionary<string, int?> result = dic.OrderBy(Data => Data.Value).ToDictionary(keyvalue => keyvalue.Key, keyvalue => keyvalue.Value);
foreach (KeyValuePair<string,int?> kvp in result)
{
Console.WriteLine(string.Format("Key - Value:{0} - {1}", kvp.Key, kvp.Value));
}
}