Export (0) Print
Expand All

SortedList Constructor (IDictionary, IComparer)

Initializes a new instance of the SortedList class that contains elements copied from the specified dictionary, has the same initial capacity as the number of elements copied, and is sorted according to the specified IComparer interface.

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

'Declaration
Public Sub New ( _
	d As IDictionary, _
	comparer As IComparer _
)

Parameters

d
Type: System.Collections.IDictionary

The IDictionary implementation to copy to a new SortedList object.

comparer
Type: System.Collections.IComparer

The IComparer implementation to use when comparing keys.

-or-

Nothing to use the IComparable implementation of each key.

ExceptionCondition
ArgumentNullException

d is Nothing.

InvalidCastException

comparer is Nothing, and one or more elements in d do not implement the IComparable interface.

The elements are sorted according to the specified IComparer implementation. If the comparer parameter is Nothing, the IComparable implementation of each key is used; therefore, each key must implement the IComparable interface to be capable of comparisons with every other key in the SortedList object.

A Hashtable object is an example of an IDictionary implementation that can be passed to this constructor. The new SortedList object contains a copy of the keys and values stored in the Hashtable.

The capacity of a SortedList object is the number of elements that the SortedList can hold. As elements are added to a SortedList, the capacity is automatically increased as required by reallocating the internal array.

If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the SortedList object.

This constructor is an O(n) operation, where n is the number of elements in d.

The following code example creates collections using different SortedList constructors and demonstrates the differences in the behavior of the collections.

Imports System
Imports System.Collections
Imports System.Globalization

Public Class SamplesSortedList

    Public Shared Sub Main()

        ' Create the dictionary. 
        Dim myHT As New Hashtable()
        myHT.Add("FIRST", "Hello")
        myHT.Add("SECOND", "World")
        myHT.Add("THIRD", "!")

        ' Create a SortedList using the default comparer. 
        Dim mySL1 As New SortedList(myHT)
        Console.WriteLine("mySL1 (default):")
        Try
            mySL1.Add("first", "Ola!")
        Catch e As ArgumentException
            Console.WriteLine(e)
        End Try
        PrintKeysAndValues(mySL1)

        ' Create a SortedList using the specified case-insensitive comparer. 
        Dim mySL2 As New SortedList(myHT, New CaseInsensitiveComparer())
        Console.WriteLine("mySL2 (case-insensitive comparer):")
        Try
            mySL2.Add("first", "Ola!")
        Catch e As ArgumentException
            Console.WriteLine(e)
        End Try
        PrintKeysAndValues(mySL2)

        ' Create a SortedList using the specified CaseInsensitiveComparer, 
        ' which is based on the Turkish culture (tr-TR), where "I" is not 
        ' the uppercase version of "i". 
        Dim myCul As New CultureInfo("tr-TR")
        Dim mySL3 As New SortedList(myHT, New CaseInsensitiveComparer(myCul))
        Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
        Try
            mySL3.Add("first", "Ola!")
        Catch e As ArgumentException
            Console.WriteLine(e)
        End Try
        PrintKeysAndValues(mySL3)

        ' Create a SortedList using the  
        ' StringComparer.InvariantCultureIgnoreCase value. 
        Dim mySL4 As New SortedList(myHT, StringComparer.InvariantCultureIgnoreCase)
        Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
        Try
            mySL4.Add("first", "Ola!")
        Catch e As ArgumentException
            Console.WriteLine(e)
        End Try
        PrintKeysAndValues(mySL4)

    End Sub 'Main

    Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
        Console.WriteLine("        -KEY-   -VALUE-")
        Dim i As Integer 
        For i = 0 To myList.Count - 1
            Console.WriteLine("        {0,-6}: {1}", _
                myList.GetKey(i), myList.GetByIndex(i))
        Next i
        Console.WriteLine()
    End Sub 'PrintKeysAndValues

End Class 'SamplesSortedList


'This code produces the following output.  Results vary depending on the system's culture settings. 

'mySL1 (default): 
'        -KEY-   -VALUE- 
'        first : Ola! 
'        FIRST : Hello 
'        SECOND: World 
'        THIRD : ! 

'mySL2 (case-insensitive comparer): 
'System.ArgumentException: Item has already been added.  Key in dictionary: 'FIRST'  Key being added: 'first''   at System.Collections.SortedList.Add(Object key, Object value) 
'   at SamplesSortedList.Main() 
'        -KEY-   -VALUE- 
'        FIRST : Hello 
'        SECOND: World 
'        THIRD : ! 

'mySL3 (case-insensitive comparer, Turkish culture): 
'        -KEY-   -VALUE- 
'        FIRST : Hello 
'        first : Ola! 
'        SECOND: World 
'        THIRD : ! 

'mySL4 (InvariantCultureIgnoreCase): 
'System.ArgumentException: Item has already been added.  Key in dictionary: 'FIRST'  Key being added: 'first''   at System.Collections.SortedList.Add(Object key, Object value) 
'   at SamplesSortedList.Main() 
'        -KEY-   -VALUE- 
'        FIRST : Hello 
'        SECOND: World 
'        THIRD : !

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft