- 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 a List in reverse order in Java
A list can be sorted in reverse order i.e. descending order using the java.util.Collections.sort() method. This method requires two parameters i.e. the list to be sorted and the Collections.reverseOrder() that reverses the order of an element collection using a Comparator.
The ClassCastException is thrown by the Collections.sort() method if there are mutually incomparable elements in the list.
A program that demonstrates this is given as follows:
Example
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Demo { public static void main(String args[]) { List aList = new ArrayList(); aList.add("James"); aList.add("Harry"); aList.add("Susan"); aList.add("Emma"); aList.add("Peter"); System.out.println("The unsorted ArrayList is: " + aList); Collections.sort(aList, Collections.reverseOrder()); System.out.println("The sorted ArrayList in reverse order is: " + aList); } }
The output of the above program is as follows:
The unsorted ArrayList is: [James, Harry, Susan, Emma, Peter] The sorted ArrayList in reverse order is: [Susan, Peter, James, Harry, Emma]
Now let us understand the above program.
The ArrayList aList is created. Then ArrayList.add() is used to add the elements to the ArrayList. The unsorted ArrayList is printed and then the ArrayList elements are sorted in reverse order using Collections.sort() and Collections.reverseOrder(). Finally, the sorted ArrayList is printed. A code snippet which demonstrates this is as follows:
List aList = new ArrayList(); aList.add("James"); aList.add("Harry"); aList.add("Susan"); aList.add("Emma"); aList.add("Peter"); System.out.println("The unsorted ArrayList is: " + aList); Collections.sort(aList, Collections.reverseOrder()); System.out.println("The sorted ArrayList in reverse order is: " + aList);
- Related Articles
- Java Program to sort a List in case sensitive order
- Java Program to sort a List in case insensitive order
- Java Program to sort Integer list in reversed order
- Java Program to Sort Array list in an Ascending Order
- Display a TreeSet in reverse order in Java
- How to sort List in descending order using Comparator in Java
- Sort list elements in descending order in C#
- Get reverse order using Comparator in Java
- Python program to sort and reverse a given list
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Sort the words in lexicographical order in Java
- C program to sort a given list of numbers in ascending order using Bubble sort
- How to print list values in reverse order using Collections.reverse() in Android?
- Program to sort a given linked list into ascending order in python
- How to sort TreeSet in descending order in Java?
