SortedList(Of TKey, TValue) Constructor (IDictionary(Of TKey, TValue), IComparer(Of TKey))

 

Initializes a new instance of the SortedList(Of TKey, TValue) class that contains elements copied from the specified IDictionary(Of TKey, TValue), has sufficient capacity to accommodate the number of elements copied, and uses the specified IComparer(Of T).

Namespace:   System.Collections.Generic
Assembly:  System (in System.dll)

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 SortedList(Of TKey, TValue).

comparer
Type: System.Collections.Generic.IComparer(Of TKey)

The IComparer(Of T) implementation to use when comparing keys.

-or-

null to use the default Comparer(Of T) for the type of the key.

Exception Condition
ArgumentNullException

dictionary is null.

ArgumentException

dictionary contains one or more duplicate keys.

Every key in a SortedList(Of TKey, TValue) must be unique according to the specified comparer; likewise, every key in the source dictionary must also be unique according to the specified comparer.

The capacity of the new SortedList(Of TKey, TValue) is set to the number of elements in dictionary, so no resizing takes place while the list is being populated.

If the data in dictionary are sorted, this constructor is an O(n) operation, where n is the number of elements in dictionary. Otherwise it is an O(n*n) operation.

The following code example shows how to use SortedList(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 SortedList(Of TKey, TValue)(IDictionary(Of TKey, TValue), IComparer(Of TKey)) constructor. In this example, the case-insensitive comparers are for the current culture.

Imports System
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main() 

        ' Create a new Dictionary of strings, with string keys and
        ' a case-insensitive equality comparer for the current 
        ' culture.
        Dim openWith As New Dictionary(Of String, String)( _
            StringComparer.CurrentCultureIgnoreCase)

        ' Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("Bmp", "paint.exe")
        openWith.Add("DIB", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")

        ' Create a SortedList of strings with string keys and a 
        ' case-insensitive equality comparer for the current culture,
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New SortedList(Of String, String)(openWith, _
            StringComparer.CurrentCultureIgnoreCase)

        ' List the sorted contents of the copy.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In copy
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0
Return to top
Show: