How can we sort elements in an array in descending order ?

 Posted by Chvrsri on 3/22/2011 | Category: C# Interview questions | Views: 5093 | Points: 40
Answer:



We have 2 different methods like

Array.Sort()
---> Which sorts the given array

Array.Reverse()---> Which reverses the array

We need to use the Array.Reverse() method after Array.Sort() method is called.Because we get a descending order only if we reverse a sorted array.

This can be done using this small example.


int[] MyArray = new int[3];
MyArray[0] = 98;
MyArray[1] = 6;
MyArray[2] = 23;
Array.Sort(MyArray);
Array.Reverse(MyArray);


Here we get an output of (98 , 23 , 6). First it sorts the given array and later it reverses which generates our required descending order.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response