How to Sort ArrayList in Java: Simple Guide with Examples
To sort an
ArrayList in Java, use Collections.sort(yourList) for natural order sorting. For custom order, use Collections.sort(yourList, comparator) where comparator defines the sorting logic.Syntax
The basic syntax to sort an ArrayList is:
Collections.sort(list);- sorts the list in natural ascending order.Collections.sort(list, comparator);- sorts the list using a custom comparator.
Here, list is your ArrayList instance, and comparator is an object that defines how to compare elements.
java
Collections.sort(list); Collections.sort(list, comparator);
Example
This example shows how to sort an ArrayList of strings in natural order and then in reverse order using a comparator.
java
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class SortArrayListExample { public static void main(String[] args) { ArrayList<String> fruits = new ArrayList<>(); fruits.add("Banana"); fruits.add("Apple"); fruits.add("Orange"); fruits.add("Mango"); // Sort in natural (alphabetical) order Collections.sort(fruits); System.out.println("Sorted list (natural order): " + fruits); // Sort in reverse order using Comparator Collections.sort(fruits, Comparator.reverseOrder()); System.out.println("Sorted list (reverse order): " + fruits); } }
Output
Sorted list (natural order): [Apple, Banana, Mango, Orange]
Sorted list (reverse order): [Orange, Mango, Banana, Apple]
Common Pitfalls
Common mistakes when sorting an ArrayList include:
- Trying to sort a list of objects without implementing
Comparableor providing aComparator. - Modifying the list while sorting, which can cause
ConcurrentModificationException. - Assuming
Collections.sort()sorts in descending order by default (it sorts ascending by default).
Always ensure your objects are comparable or provide a comparator to avoid runtime errors.
java
import java.util.ArrayList; import java.util.Collections; class Person { String name; Person(String name) { this.name = name; } } public class WrongSort { public static void main(String[] args) { ArrayList<Person> people = new ArrayList<>(); people.add(new Person("Alice")); people.add(new Person("Bob")); // This will cause a compile-time error because Person does not implement Comparable // Collections.sort(people); // WRONG // Correct way: provide a Comparator Collections.sort(people, (p1, p2) -> p1.name.compareTo(p2.name)); System.out.println("Sorted people by name."); } }
Output
Sorted people by name.
Quick Reference
Here is a quick summary of sorting methods for ArrayList:
| Method | Description |
|---|---|
| Collections.sort(list) | Sorts list in natural ascending order |
| Collections.sort(list, comparator) | Sorts list using custom comparator |
| list.sort(comparator) | Java 8+ method to sort list with comparator |
| Comparator.reverseOrder() | Comparator for reverse (descending) order |
Key Takeaways
Use Collections.sort(list) to sort an ArrayList in natural ascending order.
Provide a Comparator to Collections.sort() for custom sorting logic.
Ensure objects implement Comparable or use a Comparator to avoid errors.
Collections.sort() modifies the original list; it does not return a new list.
For reverse order, use Comparator.reverseOrder() with Collections.sort().