![]() ![]() This is passed in as a second parameter in the sort() method so that the sorted array can be rearranged in descending order. To sort an array in descending order, we used the reverseOrder() method provided by the Collections class. The sort() method takes in the array to be sorted as a parameter. We can sort arrays in ascending order using the sort() method which can be accessed from the Arrays class. Arrays can be sorted in ascending or descending order. In this article, we talked about sorting arrays in Java. When looped through and printed, the array would look like this: 10, 8, 5, 2, 1. The second parameter â Collections.reverseOrder() â will then reverse the order of the sorted array so it is arranged in descending order. Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.The first parameter is the array arr which will be sorted in ascending order. And Arrays. To sort the array in descending order, we did this: Arrays.sort(arr, Collections.reverseOrder()). 6 Answers Sorted by: 6 Collections.reverseOrder () returns a Comparator
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |