Sort an array in reverse order
| C# Code Snippets |
| See also |
| edit |
This C# code snippet sorts an array into reverse (descending) order using the IComparer interface.
using System; using System.Collections; public class ReverseArraySort { public class ReverseComparer: IComparer { public int Compare (Object object1, Object object2) { return -((IComparable) object1).CompareTo (object2); } } public static void Main() { string[] strings = {"beta", "alpha", "gamma"}; Console.WriteLine ("Array elements: "); DisplayArray (strings); Array.Sort (strings, new ReverseComparer()); DisplayArray (strings); } public static void DisplayArray (Array array) { foreach (object o in array) { Console.Write ("{0} ", o); } Console.WriteLine(); } }
| ICollection Count example (program output) |
| Array elements: beta alpha gamma gamma beta alpha |