Sorted List

The SortedList provides the features of an ArrayList and Hashtable. Here we can
access the elements in a sorted list using a key like a Hashtable or by using an index like ArrayList.

It is called SortedList because the items in the collection are in sorted order by the key.

Example 4: SortedList collection adding and reading the elements in sort order.

 

using System;
namespace SampleProgram {
  class MySample {
    public static void Main(string[] args) {
      SortedList stList = new SortedList();
      stList.Add( " 002 ", " Australia ");
      stList.Add( " 008 "," England ");
      stList.Add( " 003 ", " France ");
      stList.Add( " 009 ", " Italy ");
      stList.Add( " 004 ", " Germany");
      foreach(var k in stList.Keys) {
        Console.WriteLine(k + " : " + stList[k]);
      }
    }
  }
}

 

The output will be:

002: Australia
003: France
004: Germany
008: England
009: Italy

 

Related Tutorials