Java sort ArrayList using comparator example shows how to sort an ArrayList using a custom comparator by object properties in ascending or descending order.
How to sort ArrayList using Comparator?
We are going to store the Student objects in the ArrayList as given below.
1 2 3 4 5 6 7 8 9 |
//create an ArrayList ArrayList<Student> aListStudents = new ArrayList<Student>(); //add student objects to ArrayList aListStudents.add( new Student("01", "Student1", "10th", 410) ); aListStudents.add( new Student("02", "Student2", "10th", 290) ); aListStudents.add( new Student("03", "Student3", "10th", 430) ); aListStudents.add( new Student("04", "Student4", "10th", 375) ); aListStudents.add( new Student("05", "Student5", "10th", 422) ); |
We want to sort the ArrayList by student’s total marks in ascending order. In order to sort the ArrayList by object properties, we will need to create a custom comparator that will compare objects based on their properties.
The class implementing the Comparator interface must implement the compare
method which returns a positive integer, zero or negative integer values.
1 |
int compare(T object1, T object2) |
For our requirement, the compare
method needs to return positive integer if object1’s total marks is greater than object2’s total marks. It should return 0 if both object’s marks are equal. And it should return negative integer is object1’s total marks are less than object2’s total marks as given below.
1 2 3 4 5 6 7 8 9 10 |
public int compare(Student student1, Student student2){ if( student1.getTotalMarks() > student2.getTotalMarks() ){ return 1; }else if( student1.getTotalMarks() < student2.getTotalMarks() ){ return -1; }else{ return 0; } } |
The ArrayList can be sorted by a custom comparator using the sort
method of Collections class.
1 |
static void sort(List<T> list, Comparator<T>) |
This method sorts the List or ArrayList by the specified custom comparator.
Here is the complete example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.javacodeexamples.basic.sorting; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import com.javacodeexamples.common.Student; public class SortArrayListExample { public static void main(String[] args) { //create an ArrayList ArrayList<Student> aListStudents = new ArrayList<Student>(); //add student objects to ArrayList aListStudents.add( new Student("01", "Student1", "10th", 410) ); aListStudents.add( new Student("02", "Student2", "10th", 290) ); aListStudents.add( new Student("03", "Student3", "10th", 430) ); aListStudents.add( new Student("04", "Student4", "10th", 375) ); aListStudents.add( new Student("05", "Student5", "10th", 422) ); //print unsorted ArrayList System.out.println("Unsorted ArrayList"); for(Student student : aListStudents){ System.out.println(student); } //sort arraylist Collections.sort(aListStudents, new StudentComparator()); //print sorted arraylist System.out.println("Sorted ArrayList"); for(Student student : aListStudents){ System.out.println(student); } } } class StudentComparator implements Comparator<Student>{ public int compare(Student student1, Student student2){ if( student1.getTotalMarks() > student2.getTotalMarks() ){ return 1; }else if( student1.getTotalMarks() < student2.getTotalMarks() ){ return -1; }else{ return 0; } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 |
Unsorted ArrayList [01 : Student1 : 10th : 410] [02 : Student2 : 10th : 290] [03 : Student3 : 10th : 430] [04 : Student4 : 10th : 375] [05 : Student5 : 10th : 422] Sorted ArrayList [02 : Student2 : 10th : 290] [04 : Student4 : 10th : 375] [01 : Student1 : 10th : 410] [05 : Student5 : 10th : 422] [03 : Student3 : 10th : 430] |
How to sort ArrayList in descending order?
In the above example, we returned positive, zero and negative values for greater, equal and less object values respectively. In order to sort the list elements in descending order, we need to inverse the return values.
That is, we need to return a negative value if object1’s total marks property is greater than object2’s total marks, zero for equal, and positive value if object1’s total marks property is less than object2’s total marks as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class StudentComparator implements Comparator<Student>{ public int compare(Student student1, Student student2){ if( student1.getTotalMarks() > student2.getTotalMarks() ){ return -1; }else if( student1.getTotalMarks() < student2.getTotalMarks() ){ return 1; }else{ return 0; } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 |
Unsorted ArrayList [01 : Student1 : 10th : 410] [02 : Student2 : 10th : 290] [03 : Student3 : 10th : 430] [04 : Student4 : 10th : 375] [05 : Student5 : 10th : 422] Sorted ArrayList [03 : Student3 : 10th : 430] [05 : Student5 : 10th : 422] [01 : Student1 : 10th : 410] [04 : Student4 : 10th : 375] [02 : Student2 : 10th : 290] |
This example is a part of the Java ArrayList tutorial.
Please let me know your views in the comments section below.