0 votes
1 view
in Java by (9.2k points)

With the help of Arrays class, I can sort the array in ascending order. Can anyone tell me is there a way to sort it in descending order?

1 Answer

0 votes
by (19k points)

Check the code implementation below: 

sort(T[] a, Comparator<? super T> c) 

Arrays.sort(a, Collections.reverseOrder());


 

This works with 'Array of Objects' like Integer array. It doesn’t work for a primitive array such as an int array.

If you want to sort a primitive array in descending order, you have to first sort the array in ascending order and then reverse the array. This holds true for two-dimensional primitive arrays as well. 

Interested in Java? Check out this Java Certification by Intellipaat. 

Related questions

0 votes
1 answer
asked May 4 in Java by sheela_singh (9.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Jul 22, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...