Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Create SortedList sorted according to the specified IComparer

using System; using System.Collections; using System.Globalization; public class SamplesSortedList {     public static void Main()     {         SortedList mySL1 = new SortedList();         Console.WriteLine("mySL1 (default):");         mySL1.Add("A", "a");         mySL1.Add("B", "b");         mySL1.Add("C", "c");         try         {             mySL1.Add("first", "aaaa");         }         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));         }     } }