Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k 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 (19.7k 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, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Jul 22, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...