Why do we need Comparable and Comparator?
In the below example we have a Student class which has properties like roll no.,Student name and Student age.
public class Student { private String studentname; private int rollno; private int studentage; public Student(int rollno, String studentname, int studentage) { this.rollno = rollno; this.studentname = studentname; this.studentage = studentage; } public String getStudentname() { return studentname; } public void setStudentname(String studentname) { this.studentname = studentname; } public int getRollno() { return rollno; } public void setRollno(int rollno) { this.rollno = rollno; } public int getStudentage() { return studentage; } public void setStudentage(int studentage) { this.studentage = studentage; } }
We will add Students to the ArrayList object.
import java.util.*; public class ArrayListSort { public static void main(String args[]) { ArrayList<Student> arraylist = new ArrayList<Student>(); arraylist.add(new Student(111, "John", 23)); arraylist.add(new Student(222, "Messi", 29)); arraylist.add(new Student(333, "Ronaldo", 31)); Collections.sort(arraylist); for(Student str: arraylist){ System.out.println(str); } } }
We tried to call the Collections.sort() method on List of Objects and got the following error message.
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Bound mismatch: The generic method sort(List) of type Collections is not applicable for the arguments (ArrayList). The inferred type Student is not a valid substitute for the bounded parameter > at ArrayListSort.main(ArrayListSort.java:15)
Reason : I just called the sort method on an ArrayList of Objects which doesn't work until unless we use interfaces like Comparable and Comparator. Now we will use Comparable and Comparator to get the sorting done in our way.
Sorting of ArrayList(Object) Using Comparable
Sorting of ArrayList(Object) Using Comparable
Suppose we need to sort the ArrayList
public class Student implements Comparable { private String studentname; private int rollno; private int studentage; public Student(int rollno, String studentname, int studentage) { this.rollno = rollno; this.studentname = studentname; this.studentage = studentage; } ... //getter and setter methods same as the above example ... @Override public int compareTo(Student comparestu) { int compareage=((Student)comparestu).getStudentage(); /* For Ascending order*/ return this.studentage-compareage; /* For Descending order do like this */ //return compareage-this.studentage; } @Override public String toString() { return "[ rollno=" + rollno + ", name=" + studentname + ", age=" + studentage + "]"; } }
Now we can call Collections.sort() on ArrayList
import java.util.*; public class ArrayListSort { public static void main(String args[]) { ArrayList<Student> arraylist = new ArrayList<Student>(); arraylist.add(new Student(222, "Messi", 29)); arraylist.add(new Student(333, "Ronaldo", 31)); arraylist.add(new Student(111, "john", 23)); Collections.sort(arraylist); for(Student str: arraylist){ System.out.println(str); } } }
Output
[ rollno=111, name=John, age=23] [ rollno=222, name=Messi, age=29] [ rollno=333, name=Ronaldo, age=31]
Why do we need Comparator when we Already have Comparable
If you want to have more than one way of sorting your class, you must implement Comparator.
Read Also : Difference between Comparable and Comparator in Java
Sorting ArrayList(Object) Multiple Properties Using Comparator
To implement Comparator we need to override compare method for sorting.
import java.util.Comparator; public class Student { private String studentname; private int rollno; private int studentage; public Student(int rollno, String studentname, int studentage) { this.rollno = rollno; this.studentname = studentname; this.studentage = studentage; } ... //Getter and setter methods same as the above examples ... /*Comparator for sorting the list by Student Name*/ public static Comparator<Student> StuNameComparator = new Comparator<Student>() { public int compare(Student s1, Student s2) { String StudentName1 = s1.getStudentname().toUpperCase(); String StudentName2 = s2.getStudentname().toUpperCase(); //ascending order return StudentName1.compareTo(StudentName2); //descending order //return StudentName2.compareTo(StudentName1); }}; /*Comparator for sorting the list by roll no*/ public static Comparator<Student> StuRollno = new Comparator<Student>() { public int compare(Student s1, Student s2) { int rollno1 = s1.getRollno(); int rollno2 = s2.getRollno(); /*For ascending order*/ return rollno1-rollno2; /*For descending order*/ //rollno2-rollno1; }}; @Override public String toString() { return "[ rollno=" + rollno + ", name=" + studentname + ", age=" + studentage + "]"; }
ArrayList Class
import java.util.*; public class ArrayListSort { public static void main(String args[]){ ArrayList<Student> arraylist = new ArrayList<Student>(); arraylist.add(new Student(111, "John", 30)); arraylist.add(new Student(333, "Ronaldo", 31)); arraylist.add(new Student(222, "Messi", 29)); /*Sorting based on Student Name*/ System.out.println("Student Name Sorting:"); Collections.sort(arraylist, Student.StuNameComparator); for(Student str: arraylist){ System.out.println(str); } /* Sorting on Rollno property*/ System.out.println("RollNum Sorting:"); Collections.sort(arraylist, Student.StuRollno); for(Student str: arraylist){ System.out.println(str); } } }
Output
Student Name Sorting: [ rollno=111, name=John, age=30] [ rollno=222, name=Messi, age=29] [ rollno=333, name=Ronaldo, age=31] RollNum Sorting: [ rollno=222, name=Messi, age=29] [ rollno=111, name=John, age=30] [ rollno=333, name=Ronaldo, age=31]