Snippets Zone

Program to sort an array in descending order in C#

By Anoop Kumar Sharma    768  01-July-2021

This snippet shows how to sort an array in descending order in C#


In this snippet, we will see How to sort an array in descending order in C#. The basic concept in the program is to sort an array in ascending order and then reverse the array to change the order to descending.

Program to sort an array in descending order in C#:

static void Main(string[] args)
{
            //Initial Array
            int[] arr = { 1, 6, 4, 3, 5, 2, 9, 8, 7, 10 };
            Console.WriteLine("Intial Array:");
            //Print all values of array on console
            foreach (int val in arr)
            {
                Console.WriteLine(val);
            }
            //Sort the array
            Array.Sort(arr);
            //Reverse the order of the elements in a one dimensional array
            Array.Reverse(arr);
            Console.WriteLine("Array sorted in descending order:");
            //Print all values of array on console
            foreach (int val in arr)
            {
                Console.WriteLine(val);
            }
            //Console.ReadLine() to hold the screen after the execution of the program
            Console.ReadLine();
}

Preview:

I hope this program will help you in your interview/exam.