C# SortedList SortedList(IComparer)
Description
SortedList SortedList(IComparer)
initializes a new
instance of the SortedList class that is empty, has the default initial capacity,
and is sorted according to the specified IComparer interface.
Syntax
SortedList.SortedList(IComparer)
has the following syntax.
public SortedList(
IComparer comparer
)
Parameters
SortedList.SortedList(IComparer)
has the following parameters.
comparer
- The IComparer implementation to use when comparing keys.comparer
- -or-comparer
- null to use the IComparable implementation of each key.
Example
using System;// w w w . j a v a2 s .com
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
SortedList mySL2 = new SortedList(new CaseInsensitiveComparer(), 3);
Console.WriteLine("mySL2 (case-insensitive comparer):");
mySL2.Add("FIRST", "Hello");
mySL2.Add("SECOND", "World");
mySL2.Add("THIRD", "!");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine("{0,-6}: {1}",myList.GetKey(i), myList.GetByIndex(i));
}
}
}
The code above generates the following result.