Given an unsorted ArrayList, the task is to sort this ArrayList in ascending order in Java.
Input: Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal]
Output: Sorted ArrayList: [A computer portal, For, ForGeeks, Geeks, GeeksForGeeks]
Input: Unsorted ArrayList: [Geeks, For, ForGeeks]
Output: Sorted ArrayList: [For, ForGeeks, Geeks]
Approach: An ArrayList can be Sorted by using the sort() method of the Collections Class in Java. This sort() method takes the collection to be sorted as the parameter and returns a Collection sorted in the Ascending Order by default.
Below is the implementation of the above approach:
Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal] Sorted ArrayList in Ascending order : [A computer portal, For, ForGeeks, Geeks, GeeksForGeeks]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Sort first half in ascending and second half in descending order | Set 2
- Sort an array of string of dates in ascending order
- How to sort an ArrayList in Descending Order in Java
- ArrayList of ArrayList in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.Arraylist.indexOf() in Java
- How to sort TreeSet in descending order in Java?
- Array vs ArrayList in Java
- ArrayList in Java
- ArrayList vs LinkedList in Java
- How to remove an element from ArrayList in Java?
- ArrayList to Array Conversion in Java : toArray() Methods
- ArrayList and LinkedList remove() methods in Java with Examples
- Vector vs ArrayList in Java
- Conversion of Array To ArrayList in Java
- Synchronization of ArrayList in Java
- Custom ArrayList in Java
- Initialize an ArrayList in Java
- ArrayList toArray() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.