How to sort one dimensional array in descending order using Array Class method?

CsharpProgrammingServer Side Programming

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 one dimensional 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("\nSorted 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
raja
Published on 18-Sep-2018 16:16:54
Advertisements