Friday, April 18, 2008

SortedList and SortedDictionary Generics

The SortedList and SortedDictionary generic classes are very similar. They are strongly typed at declaration time and as you add items to the collections they become sorted.
SortedList<string, int> list = new SortedList<string, int>();
list.Add("Jake", 2);
list.Add("Rich", 4);
list.Add("Aaron", 1);
list["Mark"] = 3; // Adds entry if not found.

foreach (KeyValuePair<string, int> pair in list)
{
    Console.WriteLine(pair.Value + " - " + pair.Key);
}
The result of the foreach looks like this:
1 - Aaron
2 - Jake
3 - Mark
4 - Rich

The code above can be swapped out with a SortedDictionary with no problems.

One of the main differences between the SortedList and the SortedDictionary is that SortedList also contains an index value in addition to a key and a value for each item in the collection. Therefore it has methods and properties around this feature.
int index = list.IndexOfKey("Aaron");
int index2 = list.IndexOfValue(3);
list.RemoveAt(3);
The SortedDictionary cannot be used in substitution of the SortedList in the code above. Because SortedList has indexes it also comes with additional overhead. So if you do not have the need for the indexes, I would stick with the SortedDictionary.
More Info: MSDN: SortedList and SortedDictionary Collection Types