C# SortedList SortedList(IDictionary, IComparer)

Description

SortedList SortedList(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.

Syntax

SortedList.SortedList(IDictionary, IComparer) has the following syntax.


public SortedList(
  IDictionary d,
  IComparer comparer
)

Parameters

SortedList.SortedList(IDictionary, IComparer) has the following parameters.

  • d - The IDictionary implementation to copy to a new SortedList object.
  • comparer - The IComparer implementation to use when comparing keys.
  • comparer - -or-
  • comparer - null to use the IComparable implementation of each key.

Example

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


using System;/*from  w  ww.  j  a  v a2s . c  om*/
using System.Collections;
using System.Globalization;

public class SamplesSortedList
{
    public static void Main()
    {
        Hashtable myHT = new Hashtable();
        myHT.Add("FIRST", "Hello");
        myHT.Add("SECOND", "World");
        myHT.Add("THIRD", "!");

        SortedList mySL2 = new SortedList(myHT, new CaseInsensitiveComparer());
        SortedList mySL4 = new SortedList(myHT, StringComparer.InvariantCultureIgnoreCase);

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack