Different ways to sort an array in descending order in C#
An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. Arranging the array’s elements from largest to smallest is termed as sorting the array in descending order. Examples:
Input : array = {5, 9, 1, 4, 6, 8}; Output : 9, 8, 6, 5, 4, 1 Input : array = {1, 9, 6, 7, 5, 9}; Output : 9 9 7 6 5 1 Input : array = {-8, 9, 7, 7, 0, 9, -9}; Output : 9 9 7 7 0 -8 -9
Method 1: Using Array.Sort() and Array.Reverse() Method First, sort the array using Array.Sort() method which sorts an array ascending order then, reverse it using Array.Reverse() method.
CSHARP
// C# program sort an array in decreasing order // using Array.Sort() and Array.Reverse() Method using System; class GFG { // Main Method public static void Main() { // declaring and initializing the array int [] arr = new int [] {1, 9, 6, 7, 5, 9}; // Sort array in ascending order. Array.Sort(arr); Console.WriteLine( "Ascending: " ); foreach ( int value in arr) { Console.Write(value + " " ); } // reverse array Array.Reverse(arr); Console.WriteLine( "\n\nDescending: " ); // print all element of array foreach ( int value in arr) { Console.Write(value + " " ); } } } |
OUTPUT
Ascending:
1 5 6 7 9 9
Descending:
9 9 7 6 5 1
Method 2: Using CompareTo() Method You can also sort an array in decreasing order by using CompareTo() method.
CSHARP
// C# program sort an array in // decreasing order using // CompareTo() Method using System; class GFG { // Main Method public static void Main() { // declaring and initializing the array int [] arr = new int [] {1, 9, 6, 7, 5, 9}; // Sort the arr from last to first. // compare every element to each other Array.Sort< int >(arr, new Comparison< int >( (i1, i2) => i2.CompareTo(i1))); // print all element of array foreach ( int value in arr) { Console.Write(value + " "); } } } |
9 9 7 6 5 1
Method 3: Using delegate Here, first Sort() the delegate using an anonymous method.
csharp
// C# program sort an array // in decreasing order using System; class GFG { // Main Method public static void Main() { // declaring and initializing the array int [] arr = new int [] {1, 9, 6, 7, 5, 9}; // Sort the arr from last to first // Normal compare is m-n Array.Sort< int >(arr, delegate ( int m, int n) { return n - m; }); // print all element of array foreach ( int value in arr) { Console.Write(value + " "); } } } |
9 9 7 6 5 1
Method 4: Using Iterative way Sort an array without using any inbuilt function by iterative way.
CSHARP
// C# program sort an array // in decreasing order using // iterative way using System; class GFG { // Main Method public static void Main() { // declaring and initializing the array int [] arr = new int [] {1, 9, 6, 7, 5, 9}; int temp; // traverse 0 to array length for ( int i = 0; i < arr.Length - 1; i++) // traverse i+1 to array length for ( int j = i + 1; j < arr.Length; j++) // compare array element with // all next element if (arr[i] < arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // print all element of array foreach ( int value in arr) { Console.Write(value + " "); } } } |
9 9 7 6 5 1
Method 5: Using LINQ descending LINQ stands for Language Integrated Query. It is a uniform query syntax which is used to retrieve and save the data from the different sources. Here, OrderByDescending sorting method is used for descending order sorting. The LINQ returns IOrderedIEnumerable, which is converted to Array using ToArray() method.
CSHARP
// C# program sort an array in decreasing // order by using LINQ OrderByDescending // method using System; using System.Linq; class GFG { // Main Method public static void Main() { // declaring and initializing the // array with 6 positive number int [] arr = new int [] {1, 9, 6, 7, 5, 9}; // Sort the arr in decreasing order // and return a array arr = arr.OrderByDescending(c => c).ToArray(); // print all element of array foreach ( int value in arr) { Console.Write(value + " "); } } } |
9 9 7 6 5 1