Different ways to sort an array in descending order in C#

15 Feb, 2019
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() MethodFirst, sort the array using Array.Sort() method which sorts an array ascending order then, reverse it using Array.Reverse() method.
// C# program sort an array in decreasing order// using Array.Sort() and Array.Reverse() Methodusing 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);          // reverse array        Array.Reverse(arr);          // print all element of array        foreach(int value in arr)        {            Console.Write(value + " ");        }    }}
9 9 7 6 5 1

Method 2: Using CompareTo() MethodYou can also sort an array in decreasing order by using CompareTo() method.
// C# program sort an array in // decreasing order using // CompareTo() Methodusing 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 delegateHere, first Sort() the delegate using an anonymous method.
// C# program sort an array // in decreasing orderusing 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.
// C# program sort an array // in decreasing order using // iterative wayusing 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 descendingLINQ 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.
// C# program sort an array in decreasing // order by using LINQ OrderByDescending // methodusing 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

CSharp-Arrays
Technical Scripter 2018
C#
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
Introduction to .NET Framework
C# | Delegates
C# | Multiple inheritance using interfaces
Differences Between .NET Core and .NET Framework
C# | Data Types
C# | Method Overriding
C# | String.IndexOf( ) Method | Set - 1
C# | Constructors
C# | Class and Object