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

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



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("
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
Updated on: 2020-06-23T12:38:54+05:30

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements