Array - Sort
Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.
public static void Main() { int[] sourceArray = { 12, 7, 32, 8, 6, 5, 4, 19, 15, 5 }; // Displays the values of the Array. Console.WriteLine( "Original values:" ); PrintValues(sourceArray); // C# Extension Method: Array - Sort sourceArray.Sort(); Console.WriteLine( "Values after calling Sort method:" ); PrintValues(sourceArray); } public static void PrintValues(Array myArr) { int i = 0; int cols = myArr.GetLength(myArr.Rank - 1); foreach (object o in myArr) { if ( i < cols ) { i++; } else { Console.WriteLine(); i = 1; } Console.Write( "\t{0}", o); } Console.WriteLine(); }
View Source
using System; public static partial class Extensions { /// <summary> /// Reverses the sequence of the elements in the entire one-dimensional . /// </summary> /// <param name="array">The one-dimensional to reverse.</param> public static void Reverse(this Array array) { Array.Reverse(array); } /// <summary> /// Reverses the sequence of the elements in a range of elements in the one-dimensional . /// </summary> /// <param name="array">The one-dimensional to reverse.</param> /// <param name="index">The starting index of the section to reverse.</param> /// <param name="length">The number of elements in the section to reverse.</param> public static void Reverse(this Array array, Int32 index, Int32 length) { Array.Reverse(array, index, length); } }