- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort items of an ArrayList with Collections.reverseOrder() in Java
In order to sort items of an ArrayList with Collections.reverseOrder() in Java, we need to use the Collections.reverseOrder() method which returns a comparator which gives the reverse of the natural ordering on a collection of objects that implement the Comparable interface.
Declaration − The java.util.Collections.reverseOrder() method is declared as follows -
public static <T> Comparator<T> reverseOrder()
Let us see a program to sort an ArrayList with Collections.reverseOrder() in Java -
Example
import java.util.*; public class Example { public static void main (String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(50); list.add(30); list.add(20); list.add(40); list.add(60); System.out.println("Original list : " + list); Collections.sort(list); System.out.println("Sorted list : " + list); Collections.sort(list,Collections.reverseOrder()); System.out.println("Sorted list using Collections.reverseOrder() : " + list); } }
Output
Original list : [10, 50, 30, 20, 40, 60] Sorted list : [10, 20, 30, 40, 50, 60] Sorted list using Collections.reverseOrder() : [60, 50, 40, 30, 20, 10]
- Related Articles
- Sort Elements in an ArrayList in Java
- Remove duplicate items from an ArrayList in Java
- How to sort an ArrayList in Java in ascending order?
- How to sort an ArrayList in Java in descending order?
- How to sort an ArrayList in Ascending Order in Java
- How to sort an ArrayList in Descending Order in Java
- Sort ArrayList in Descending order using Comparator with Java Collections
- Sort items in a Java TreeSet
- How to sort an ArrayList in C#?
- Sort items in MySQL with dots?
- Java Program to Sort ArrayList of Custom Objects by Property
- Search an element of ArrayList in Java
- Clear an ArrayList in Java
- Clone an ArrayList in Java
- Initialize an ArrayList in Java

Advertisements