Sorting an ArrayList of Custom objects by property is a common thing in Java programming when you deal with a large set of data. Sometimes, you may want to sort an ArrayList based on one or more properties of the objects. Java simply offers many ways to achieve this, which you will be learning in this blog. Let’s start learning with the basics.
Table of Contents:
What is an ArrayList in Java?
Java ArrayList is a part of the collections framework, and it is a class in java.util package. It provides us with the dynamic-sized arrays in Java.
An ArrayList is a dynamic array that can hold a collection of objects. Java provides several ways to achieve this, the most common is by using the Comparator interface or the lambda expressions
Syntax of creating an empty ArrayList:
ArrayList<E> list = new ArrayList<>();
Different Ways to Sort an ArrayList of Objects by Property
There are mainly two different approaches to sorting an ArrayList of custom objects. These are:
- Using Comparator Interface
- Using Comparable Interface
Let us understand both of these in detail.
1. Using the Comparator Interface
The Comparator interface is used when you need to define the custom sorting logic that is separate from that of the object’s class. It is flexible because you can create different comparators for different sorting criteria (e.g., by age, by name, etc.) and pass them to the sorting methods.
Example:
Output:
Explanation: In the above code, the Comparator interface is used to sort the Person objects by age. A Comparator is passed to Collections.sort() with a compare method that compares the ages. This allows sorting of the ArrayList based on the age.
2. Using the Comparable Interface
The Comparable interface is used when the natural ordering of the object is defined in the class itself. This means that you can implement the compareTo method inside the object’s class, and it defines how the objects of that class should be sorted by default.
Example:
Output:
Explanation: In the above code, the Person class implements a Comparable interface to compare the objects by age. The compareTo method sorts the Person objects using Collections.sort(people).
Differences between the Comparator and Comparable Interface in Java
Here are the following differences between comparator and comparable in Java:
Feature | Comparator | Comparable |
Purpose | Used for custom sorting of objects. | Defines the natural ordering of objects. |
Location of Sorting Logic | External to the class, in a separate class or method. | Defined inside the class itself. |
Method Used | compare(T o1, T o2) | compareTo(T o) |
Flexibility | Very flexible. Multiple comparators can be created. | Limited flexibility. Only one natural order is allowed. |
Usage | Can sort by various properties using different comparators. | Sorts are based on a single property (natural ordering). |
Modifying the Class | Does not require modifying the custom class. | Requires modifying the class to implement Comparable. |
Multiple Sorting Criteria | Can define multiple sorting criteria with different comparators. | Can only define one natural sorting order. |
Sorting Example | Collections.sort(list, new Comparator<>() {…}) | Collections.sort(list) or list.sort() (if compareTo is implemented). |
Common Usage | Sorting by various properties, sorting in reverse order, and custom sorting logic. | Sorting by a default property (e.g., sorting by age or name). |
Default Sorting | No natural order is defined. You must explicitly define a comparison. | Provides the natural ordering of objects within the class. |
Conclusion
In Java, sorting an ArrayList of custom objects can be done by using both the Comparator and Comparable interfaces. The Comparator allows for the custom sorting defined outside the object class, providing flexibility for different sorting criteria. On the other hand, the Comparable interface defines the natural sorting order within the object class itself. Both methods help in organizing and sorting the data efficiently in Java.
If you want to learn more about Java, you can refer to our Java Course.
Sort ArrayList of custom Objects by property – FAQs
Q1. How to sort the list of custom objects in Java?
You can also sort a list of custom objects using the Collections. sort() method
Q2. How to sort an ArrayList based on a parameter?
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 Ascending Order.
Q3. Which interface should be implemented for sorting?
The Comparable interface can be used to provide a single way of sorting, whereas the Comparator interface is used to provide different ways of sorting.
Q4. Can an ArrayList be a parameter?
Yes, you can pass an ArrayList of custom objects as a parameter in Java
Q5. What is the fastest way to sort a list?
QuickSort is usually the fastest sorting algorithm. Its performance is measured most of the time in O(N × log N).