Sort array descending in C#
To sort an array in descending order in C# first you need to use theArray.Sort()
method which will automatically sort the array in ascending order then reverse the array using Array.Reverse()
method.Example
using System;
public class Program
{
public static void Main()
{
int[] arr = new int[] {3, 2, 1, 3};
Array.Sort(arr);
Array.Reverse(arr);
Console.WriteLine(arr[0]);
Console.WriteLine(arr[1]);
Console.WriteLine(arr[2]);
Console.WriteLine(arr[3]);
}
}
Output
3
3
2
1
3
2
1