SortedDictionary(Of TKey, TValue) Constructor (IDictionary(Of TKey, TValue), IComparer(Of TKey))
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Initializes a new instance of the SortedDictionary(Of TKey, TValue) class that contains elements copied from the specified IDictionary(Of TKey, TValue) and uses the specified IComparer(Of T) implementation to compare keys.
Assembly: System (in System.dll)
'Declaration Public Sub New ( _ dictionary As IDictionary(Of TKey, TValue), _ comparer As IComparer(Of TKey) _ )
Parameters
- dictionary
- Type: System.Collections.Generic.IDictionary(Of TKey, TValue)
The IDictionary(Of TKey, TValue) whose elements are copied to the new SortedDictionary(Of TKey, TValue).
- comparer
- Type: System.Collections.Generic.IComparer(Of TKey)
The IComparer(Of T) implementation to use when comparing keys, or Nothing to use the default Comparer(Of T) for the type of the key.
| Exception | Condition |
|---|---|
| ArgumentNullException | dictionary is Nothing. |
| ArgumentException | dictionary contains one or more duplicate keys. |
Every key in a SortedDictionary(Of TKey, TValue) must be unique according to the specified comparer; therefore, every key in the source dictionary must also be unique according to the specified comparer.
SortedDictionary(Of TKey, TValue) requires a comparer implementation to perform key comparisons. If comparer is Nothing, this constructor uses the default generic equality comparer, Comparer(Of T).Default. If type TKey implements the System.IComparable(Of T) generic interface, the default comparer uses that implementation.
This constructor is an O(n log n) operation, where n is the number of elements in dictionary.
The following code example shows how to use SortedDictionary(Of TKey, TValue) to create a case-insensitive sorted copy of the information in a case-insensitive Dictionary(Of TKey, TValue), by passing the Dictionary(Of TKey, TValue) to the SortedDictionary(Of TKey, TValue)(IDictionary(Of TKey, TValue), IComparer(Of TKey)) constructor. In this example, the case-insensitive comparers are for the current culture.