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
Release: 2024-12-26 11:39:17
Original
557 people have browsed it

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template