Sorting generic Dictionaries
A colleague (Daniel Wessels) just asked me how I would sort a generic Dictionary because it has no build in Sort method. A solution that uses functionality that is built in would be to (ab)use a generic list:
var dictionary =
new Dictionary<string, int>()
{
{"George",42},
{"Mike",28},
{"Pete", 56},
{"Sam", 33},
};
var list = dictionary.ToList();
list.Sort((a, b) => a.Value.CompareTo(b.Value));
foreach (var keyValuePair in list)
{
Console.WriteLine(
string.Concat(
keyValuePair.Key,
" ",
keyValuePair.Value));
}