Create SortedList

In this chapter you will learn:

  1. Create a SortedList using the default comparer
  2. Create a SortedList using the specified case-insensitive comparer
  3. SortedList with CultureInfo
  4. Create a SortedList using the StringComparer.InvariantCultureIgnoreCase value

Create a SortedList using the default comparer

using System;//  j  av  a2  s. c om
using System.Collections;
using System.Globalization;

public class SamplesSortedList
{
    public static void Main()
    {

        SortedList mySL1 = new SortedList( 3 );
        Console.WriteLine("mySL1 (default):");
        mySL1.Add("FIRST", "Hello");
        mySL1.Add("SECOND", "World");
        mySL1.Add("THIRD", "!");
        try
        {
            mySL1.Add("first", "Ola!");
        }
        catch (ArgumentException e)
        {
            Console.WriteLine(e);
        }
        PrintKeysAndValues(mySL1);

    }

    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));
        }
    }
}

Create a SortedList using the specified case-insensitive comparer

using System;//  j  a  v  a2s  . c  om
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));
        }
    }
}

SortedList with CultureInfo

Create a SortedList using the specified CaseInsensitiveComparer, which is based on the Turkish culture (tr-TR).

using System;//from  j  av  a2  s . c om
using System.Collections;
using System.Globalization;

public class SamplesSortedList
{
    public static void Main()
    {
        CultureInfo myCul = new CultureInfo("tr-TR");
        SortedList mySL3 = new SortedList(new CaseInsensitiveComparer(myCul), 3);

        Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):");

        mySL3.Add("FIRST", "Hello");
        mySL3.Add("SECOND", "World");
        mySL3.Add("THIRD", "!");
        try{
            mySL3.Add("first", "Ola!");
        }catch (ArgumentException e){
            Console.WriteLine(e);
        }
        PrintKeysAndValues(mySL3);

    }

    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));
        }
    }
}

SortedList with StringComparer.InvariantCultureIgnoreCase

using System;//from  java  2  s . c  om
using System.Collections;
using System.Globalization;

public class SamplesSortedList
{
    public static void Main()
    {
        SortedList mySL4 = new SortedList(StringComparer.InvariantCultureIgnoreCase, 3);

        Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
        mySL4.Add("FIRST", "Hello");
        mySL4.Add("SECOND", "World");
        mySL4.Add("THIRD", "!");
        try
        {
            mySL4.Add("first", "Ola!");
        }
        catch (ArgumentException e)
        {
            Console.WriteLine(e);
        }
        PrintKeysAndValues(mySL4);

    }

    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));
        }
    }
}

Next chapter...

What you will learn in the next chapter:

  1. How to get key index in SortedList
  2. How to get the index of a value in SortedList
  3. How to get value by index