How to Sort an ArrayList of Objects by Property in Java?
ArrayList in Java (equivalent to vector in C++) having a dynamic size. It can be shrinked or expanded based on size. ArrayList is a part of the collection framework and is present in java.util package.
--> java.util Package --> ArrayList Class
Syntax: Creating an empty ArrayList
ArrayList <E> list = new ArrayList <> ();
Different Ways to Sort an ArrayList of Objects by Property
- Using Comparator interface
- Using Comparable interface
In this case, the comparator is a lambda which defines the below parameters as follows:
- Takes two objects from the list o1 and o2.
- Compares the two object’s customProperty using compareTo() method.
- And finally returns a positive number if o1’s property is greater than o2’s, negative if o1’s property is lesser than o2’s, and zero if they are equal.
- Based on this, the list is sorted based on the least property to the greatest and stored back on to list.
- In the below program, we’ve defined a CustomObject class with a String property, customProperty.
- We’ve also added a constructor that initializes the property, and a getter function getCustomProperty() which returns customProperty.
- In the main() method, we’ve created an array list of custom objects list, initialized with 5 objects.
- For sorting the list with the given property, we use the list‘s sort() method.
- The sort() method takes the list to be sorted (final sorted list is also the same) and a comparator.
A Aa B X Z
Approach 2: Using Comparable and comparator
When the ArrayList is of a custom object type, then, in this case, we use two sorting methods by either Comparator or Comparable and in this case Collections.sort() cannot be used directly as it will give an error because it sorts only specific data-types and not user-defined types.
2-A: Sorting ArrayList with Comparable
- The custom object type class which is Student here will implement the Comparable class<Student>.
- This will override the compareTo() method of Comparable class which takes the object of class Student as a parameter, and we need to compare the values/ attributes by which we want to sort the list and return accordingly in the compareTo() function.
[ rollno=12, name=Riya, age=15] [ rollno=13, name=Shubhi, age=15] [ rollno=14, name=Mahima, age=16]
2-B: Sorting ArrayList with Comparator
- We will define another class that will implement the Comparator class of the type of our custom object. For eg, in the below code, our custom class is Student so another class that we have defined will implement Comparatot<Student>.
- This class will override the compare method of the Comparator class which accepts two objects of the Student class as parameters and returns the comparison value according to our requirement whether we want to sort the array in ascending or descending order and on which attribute, we want to sort the list.
Student Name Sorting: [ rollno=505, name=Abey, age=24] [ rollno=809, name=Vignesh, age=32] [ rollno=101, name=Zues, age=26] RollNum Sorting: [ rollno=101, name=Zues, age=26] [ rollno=505, name=Abey, age=24] [ rollno=809, name=Vignesh, age=32]