Although I am late to the game, .NET Framework 4.5 provides new classes for you. See SortedList<TKey, TValue>
or SortedDictionary<TKey, TValue>
. If you are wondering which one you should use, the MSDN provides a few good reasons why you might choose one over the other.
The SortedList generic class is an array of key/value pairs with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedDictionary generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:
SortedList<TKey, TValue>
uses less memory than SortedDictionary<TKey, TValue>
.
SortedDictionary<TKey, TValue>
has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList<TKey, TValue>
.
- If the list is populated all at once from sorted data,
SortedList<TKey, TValue>
is faster than SortedDictionary<TKey, TValue>
.
Another difference between the SortedDictionary<TKey, TValue>
and SortedList<TKey, TValue>
classes is that SortedList<TKey, TValue>
supports efficient indexed retrieval of keys and values through the collections returned by the Keys and Values properties. It is not necessary to regenerate the lists when the properties are accessed, because the lists are just wrappers for the internal arrays of keys and values.
Both links have similar Remarks section (which is where the quote comes from). They also have more information for both classes. I'd recommend reading both sections if you are interested in using one of them.