Home >Java >javaTutorial >How to Sort an ArrayList in Descending Order in Java?

How to Sort an ArrayList in Descending Order in Java?

Barbara Streisand
Barbara StreisandOriginal
2024-12-26 11:39:17504browse

How to Sort an ArrayList in Descending Order in Java?

Sorting an ArrayList in Descending Order

In Java, we can use Collections.sort() to sort a List or ArrayList in ascending order. However, for descending order, we need to perform an additional step.

Solution:

  1. Import the Collections class (import java.util.Collections;).
  2. Call Collections.sort(list) to sort the list in ascending order.
  3. Call Collections.reverse(list) to reverse the order of the elements.

Example:

This will print the list in descending order:

The above is the detailed content of How to Sort an ArrayList in Descending Order in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn