Example
using System; using System.Collections; namespace Samples { public class ArraySample { public class MyReverseComparer: IComparer { public int Compare(Object a, Object b) { return -((IComparable)a).CompareTo(b); } } public static void Main() { string[] strings = {"one", "two", "three"}; Console.WriteLine("Array elements: "); Display(strings); Array.Reverse(strings); Display(strings); Array.Sort(strings); Display(strings); Array.Sort(strings, new MyReverseComparer()); Display(strings); } public static void Display(Array a) { foreach(object o in a) Console.Write("{0} ", o); Console.WriteLine(); } } }
Array elements: one two three three two one one three two two three one
Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.