Sorting and Reversing an Array using C# inbuilt functions
The following code snippet shows how to sort and reverse an array using C# inbuilt function Sort() and Reverse() respectively.
In C#, every array we create is automatically derived from the System.Array class. This class defines a number of methods and properties that can be used to manipulate arrays more efficiently.
Sorting and Reversing an Array
using System;
class SortReverse
{
public static void Main()
{
int[] x={30,10,80,90,20};
Console.WriteLine("Array Before Sorting");
foreach(int i in x)
Console.Write(" "+i);
Console.WriteLine();
Array.Sort(x);
Console.WriteLine("Array After Sorting");
foreach(int i in x)
Console.Write(" "+i);
Console.WriteLine();
Array.Reverse(x);
Console.WriteLine("Array After Reversing");
foreach(int i in x)
Console.Write(" "+i);
Console.WriteLine();
}
}
Output:
Array Before Sorting
30 10 80 90 20
Array After Sorting
10 20 30 80 90
Array After Reversing
90 80 30 20 10