Use Comparator For Custom Sorting

Category: Java   Tags: Java, Java Collection, Java Comparator Interface

Comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of two different classes. Comparator has this declaration:

                                    public interface Comparator<T>
                                

Methods

int compare(T o1, T o2)
It compares its two arguments o1 and o2 for ordering.

Returns -ve integer if o1 < o2
Returns 0 if o1 == o2
Returns +ve integer if o1 > o2

boolean equals(Object obj)
Returns true only if the specified object is also a comparator and it imposes the same ordering as this comparator.

Suppose we have an ArrayList whose element type is a custom class Student having fields rollNumber, name, age and we need to sort the array based on roll number or name. This is where Comparator comes into the picture. Here is one Java example that shows this:

JavaComparatorExample.java

                            package com.tutorial.java.collections;

                            import java.util.ArrayList;
                            import java.util.Collections;
                            import java.util.Comparator;

                            class Student {

                                private int rollNo;
                                private String name;
                                private int age;

                                public Student(int rollNo, String name, int age) {
                                    this.rollNo = rollNo;
                                    this.name = name;
                                    this.age = age;
                                }

                                public int getRollNo() {
                                    return rollNo;
                                }

                                public void setRollNo(int rollNo) {
                                    this.rollNo = rollNo;
                                }

                                public String getName() {
                                    return name;
                                }

                                public void setName(String name) {
                                    this.name = name;
                                }

                                public int getAge() {
                                    return age;
                                }

                                public void setAge(int age) {
                                    this.age = age;
                                }

                            }

                            class Sortbyroll implements Comparator<Student> {

                                // Sorting in ascending order of roll number
                                public int compare(Student a, Student b) {
                                    return a.getRollNo() - b.getRollNo();
                                }
                            }

                            class Sortbyname implements Comparator<Student> {

                                // Sorting in ascending order of name
                                public int compare(Student a, Student b) {
                                    return a.getName().compareTo(b.getName());
                                }
                            }

                            public class JavaComparatorExample {

                                static void details(Student student) {
                                    System.out.println(student.getRollNo() + " - " + student.getName() + " - " + student.getAge());
                                }

                                public static void main(String[] args) {
                                    ArrayList<Student> list = new ArrayList<Student>();
                                    list.add(new Student(2, "B", 16));
                                    list.add(new Student(1, "A", 18));
                                    list.add(new Student(4, "D", 17));
                                    list.add(new Student(3, "C", 15));

                                    System.out.println("Original ArrayList: ");
                                    for (int i = 0; i < list.size(); i++) {
                                        details(list.get(i));
                                    }

                                    Collections.sort(list, new Sortbyname());

                                    System.out.println("Sorted by name");
                                    for (int i = 0; i < list.size(); i++) {
                                        details(list.get(i));
                                    }

                                    Collections.sort(list, new Sortbyroll());

                                    System.out.println("Sorted by rollno");
                                    for (int i = 0; i < list.size(); i++) {
                                        details(list.get(i));
                                    }
                                }

                            }
                        

Output:

                            Original ArrayList:
                            2 - B - 16
                            1 - A - 18
                            4 - D - 17
                            3 - C - 15
                            Sorted by name
                            1 - A - 18
                            2 - B - 16
                            3 - C - 15
                            4 - D - 17
                            Sorted by rollno
                            1 - A - 18
                            2 - B - 16
                            3 - C - 15
                            4 - D - 17