Table of contents Home page
using System; using System.Collections; public class Program { private class ReverseSortIComparer : IComparer { int IComparer.Compare(Object x, Object y) { Int32 ix = (Int32)x; return -ix.CompareTo((int)y); } } public static void Main() { int[] a = { 5, 1, 3 }; Console.WriteLine("Original array"); Program.dumpArray(a); Array.Sort(a); Console.WriteLine("After sorting from small to large"); Program.dumpArray(a); Array.Sort(a, new ReverseSortIComparer()); Console.WriteLine("After sorting from large to small"); Program.dumpArray(a); Console.ReadKey(); } private static void dumpArray(int[] a) { for (int i = 0; i < a.Length; i++) { Console.WriteLine("a[{0}] = {1}", i, a[i]); } Console.WriteLine(); } }