Using the StringDictionary
class, here is a method to use LINQ's OrderBy
. Assumes you have .NET 3.5.
var sortedDictionary = dictionary.Cast<DictionaryEntry>().OrderBy(pair => pair.Value);
Using 2.0, it's a bit trickier. Here's an approach using a Comparison delegate.
StringDictionary dictionary = new StringDictionary();
dictionary.Add("1", "One");
dictionary.Add("2", "Two");
dictionary.Add("3", "Three");
DictionaryEntry[] sortedDictionary = new DictionaryEntry[dictionary.Count];
dictionary.CopyTo(sortedDictionary, 0);
Comparison<DictionaryEntry> comparison = new Comparison<DictionaryEntry>(delegate (DictionaryEntry obj1, DictionaryEntry obj2) { return ((string)obj1.Value).CompareTo((string)obj2.Value); });
Array.Sort(sortedDictionary, comparison);
So the actual sort would be in the sortedDictionary array.