- 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
How to sort an ArrayList in Java in descending order?
To sort the contents of an ArrayList in descending order
- Create an ArrayList.
- Sort the contents of the ArrayList using the sort() method of the Collections class.
- Then, reverse array list using the reverse() method of the Collections class.
Example:
import java.util.ArrayList; import java.util.Collections; public class ArrayListSample { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("JavaFx"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); Collections.sort(list); System.out.println(list); Collections.reverse(list); System.out.println(list); } }
Output:
[Java, JavaFx, OpenCV, WebGL] [WebGL, OpenCV, JavaFx, Java]
Advertisements