The SortedList generic class is a binary search tree 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 uses less memory than SortedDictionary.
-
SortedDictionary has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList.
-
If the list is populated all at once from sorted data, SortedList is faster than SortedDictionary.
Another difference between the SortedDictionary and SortedList classes is that SortedList 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. The following code shows the use of the Values property for indexed retrieval of values from a sorted list of strings:
Dim v As String = mySortedList.Values(3)
string v = mySortedList.Values[3];
String^ v = mySortedList->Values[3];
SortedList is implemented as an array of key/value pairs, sorted by the key. Each element can be retrieved as a KeyValuePair object.
Key objects must be immutable as long as they are used as keys in the SortedList. Every key in a SortedList must be unique. A key cannot be a null reference (Nothing in Visual Basic), but a value can be, if the type of values in the list, TValue, is a reference type.
SortedList requires a comparer implementation to sort and to perform comparisons. The default comparer Comparer.Default checks whether the key type TKey implements System.IComparable and uses that implementation, if available. If not, Comparer.Default checks whether the key type TKey implements System.IComparable. If the key type TKey does not implement either interface, you can specify a System.Collections.Generic.IComparer implementation in a constructor overload that accepts a comparer parameter.
The capacity of a SortedList is the number of elements the SortedList can hold. As elements are added to a SortedList, the capacity is automatically increased as required by reallocating the internal array. The capacity can be decreased by calling TrimExcess or by setting the Capacity property explicitly. Decreasing the capacity reallocates memory and copies all the elements in the SortedList.
The foreach statement of the C# language (for each in C++, For Each in Visual Basic) requires the type of the elements in the collection. Since the elements of the SortedList are key/value pairs, the element type is not the type of the key or the type of the value. Instead, the element type is KeyValuePair. For example:
foreach (KeyValuePair<int, string> kvp in mySortedList) {...}
for each (KeyValuePair<int, String^> kvp in mySortedList) {...}
For Each kvp As KeyValuePair(Of Integer, String) In mySortedList
...
Next kvp
The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.