How to sort ArrayList in Java: Find the best solution for this in this article.
Let’s explore different approaches for sorting elements of an ArrayList in this post.
ArrayList is one of the widely used collection classes of the Collection Framework in Java.
ArrayList is nothing but a List implementation that implements a dynamic array to store the elements internally.
Thus, an ArrayList will dynamically grow and reduce as long as you add and remove elements.
Contents
How To Sort ArrayList in Java
ArrayList Is Widely used classes of the framework in Java. Let’s check out the best approaches to sort ArrayList in Java in every order Ascending order, Descending order, integer objects, and more. To sort the Arraylist in Java you need to call to collection.sort() methods.
You may like,
Whether Number or String is Palindrome in Java.
Sorting ArrayList of String Objects
Assume an ArrayList that holds State names as String Objects.
In order to sort the ArrayList, we need to call a method Collections.sort().
Here we need to pass the ArrayList object as an argument to the Collections.sort() method.
Note that, by default, this method returns the sorted list of String in ascending order alphabetically.
If You have any doubts about How to sort ArrayList in java, feel free to comment on us, we will definitely answer your doubt.
Sort String in Ascending Order
Let’s write the code to sort the ArrayList of String in Ascending order.
Let’s Initialize the ArrayList object in the constructor and add the states to the ArrayList.
Finally pass the Arraylist object to Collections.sort() method.
import java.util.ArrayList;
import java.util.Collections;
public class Main
{
public static void main(String[] args) {
try {
ArrayList stateList = new ArrayList<>();
stateList.add("Florida");
stateList.add("Illinois");
stateList.add("Alabama");
stateList.add("Texas");
Collections.sort(stateList);
System.out.println("Sorted Results : "+stateList);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Sorted Results : [Alabama, Florida, Illinois, Texas]
Here the output above is in List of String format, you can also convert the List to String in Java as per the need.
Sort String in Descending Order
Let’s write the code to sort the ArrayList of String in Descending order.
Apart from passing the ArrayList object, here we also need to pass a Comparator argument.
So we will pass Collections.reverseOrder() as comparator in below code.
import java.util.ArrayList;
import java.util.Collections;
public class Main
{
public static void main(String[] args) {
try {
ArrayList stateList = new ArrayList<>();
stateList.add("Florida");
stateList.add("Illinois");
stateList.add("Alabama");
stateList.add("Texas");
Collections.sort(stateList,Collections.reverseOrder());
System.out.println("Sorted Results : "+stateList);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Sorted Results : [Texas, Illinois, Florida, Alabama]
Sorting ArrayList of Integer Objects
it’s one of more how to sort ArrayList in java by sorting Arraylist of integer objects.
Assume an ArrayList that holds numbers as Integer Objects.
In order to sort the ArrayList, we need to call a method Collections.sort().
Here we need to pass the ArrayList object as an argument to the Collections.sort() method.
Note that, by default, this method returns the sorted list of Integer in ascending order.
Sort Integer in Ascending Order
Let’s write the code to sort the ArrayList of Integer in Ascending order.
Let’s Initialize the ArrayList object in the constructor and add the numbers to the ArrayList.
Finally pass the Arraylist object to Collections.sort() method.
import java.util.ArrayList;
import java.util.Collections;
public class Main
{
public static void main(String[] args) {
try {
ArrayList numberList = new ArrayList<>();
numberList.add(7);
numberList.add(5);
numberList.add(15);
numberList.add(3);
Collections.sort(numberList);
System.out.println("Sorted Results : "+numberList);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Sorted Results : [3, 5, 7, 15]
Sort Integer in Descending Order
Our last but not the least on how to sort ArrayList in java by the integer descending order.
Let’s write the code to sort the ArrayList of String in Descending order.
Apart from passing the ArrayList object, here we also need to pass a Comparator argument.
So we will pass Collections.reverseOrder() as comparator in below code.
import java.util.ArrayList;
import java.util.Collections;
public class Main
{
public static void main(String[] args) {
try {
ArrayList numberList = new ArrayList<>();
numberList.add(7);
numberList.add(5);
numberList.add(15);
numberList.add(3);
Collections.sort(numberList,Collections.reverseOrder());
System.out.println("Sorted Results : "+numberList);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Sorted Results : [15, 7, 5, 3]
Conclusion
Above are some of the best approaches w.r.t how to sort ArrayList in java.
To conclude this tutorial, we covered various types of implementation to sort ArrayList in Java.