List Sorting in Java

Asked

Viewed 69 times

-2

If Collections.sort(lista); sort the list ascendingly, which command can I use to leave a list in descending order?

  • Present a [mcve] of your code so that it is possible to suggest something based on what you have done, and so that the question does not look like a code request ready, which is not welcome here.

  • @Hexacampão articuno Actually I just needed to know what was the inverse of Collections.sort(lista); that I eventually discovered is Collections.reverse(teste); but thank you for the reply.

  • 2

    Note that the method Collections.reverse() will not sort the list, as you suggest in the question, it just reverses the list order without ordering it.

1 answer

3


There is an overload of the method Collections.sort() which takes as the second parameter an object Comparator (documentation), and the class itself Collections has another method that returns just one comparator object that imposes a reverse order on the list, the Collections.reverseOrder() (documentation).

The code goes like this:

Collections.sort(lista, Collections.reverseOrder());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.