Sample Question - How do you sort a C# dictionary by value?

I often have a Dictionary of keys & values and need to sort it by value. For example, I have a hash of words and their frequencies, and want to order them by frequency.

There's SortedList which is good for a single value (frequency), but I want to map it back to the word.

SortedDictionary orders by key, not value. Some resort to a custom class, but what's the cleanest way?

Thomas Jacobs, India
16 March 2012

1  Answer(s) About: Journal of Dharma

To sort a Dictionary by value and save it back to itself (so that when you foreach over it the values come out in order):

Vineeth Vadakethala, India
16 March 2012

Your Answer