How do you sort an array in C# in descending order?



The following is the unsorted array.

int[] list = {98, 23, 97, 36, 77};

Now first use the Sort() method to sort the array.

Array.Reverse(list);

Use the Reverse() method that would eventually give you a sorted array in descending order.

Array.Reverse(list);

You can try to run the following code to to sort an array in descending order.

Example

 Live Demo

using System;
namespace Demo {
   public class MyApplication {
      public static void Main(string[] args) {
         int[] list = {98, 23, 97, 36, 77};
         Console.WriteLine("Original Unsorted List");
         foreach (int i in list) {
            Console.Write(i + " ");
         }
         //sort
         Array.Sort(list);
         // Descending order
         Array.Reverse(list);
         Console.WriteLine("
Sorted List");          for(int i=0; i<list.Length; i++) {             Console.Write(list[i] + " ");          }       }    } }

Output

Original Unsorted List
98 23 97 36 77
Sorted List
98 97 77 36 23
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements