C# How To Sort an Array
Sort an Array
Use the Array.Sort() method to sort an array from low to high:
Example
int[] numbers = {5, 2, 8, 1, 9};
Array.Sort(numbers);
foreach (int num in numbers)
{
Console.WriteLine(num);
}
To sort from high to low, sort the array first, and then reverse it with Array.Reverse():
Example
int[] numbers = {5, 2, 8, 1, 9};
Array.Sort(numbers);
Array.Reverse(numbers);
foreach (int num in numbers)
{
Console.WriteLine(num);
}
Explanation:Array.Sort() sorts the elements of the array itself, from the lowest to the highest value.Array.Reverse() then turns the order around, so the highest value comes first.