Create SortedList from another collection and sorted by IComparable implemented by each key in CSharp

Description

The following code shows how to create SortedList from another collection and sorted by IComparable implemented by each key.

Example


using System;/*from   w  w w .ja  va  2 s .c  o  m*/
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 myList = new SortedList(myHT);


        for (int i = 0; i < myList.Count; i++)
        {
            Console.WriteLine(myList.GetKey(i)+" "+ myList.GetByIndex(i));
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary