The Comparable interface provides a compareTo() method for the ordering of objects. This ordering is called the class’s natural ordering and the compareTo() method is called its natural comparison method. The Comparator interface provides the methods for performing sorting operations. By using the Comparator interface we can do multiple sorting sequences. We can sort the objects with respect to multiple data members.
compareTo() method compares this object with an o1 object and returns an integer.
public int compareTo(Object o1)
import java.util.*; class Employee implements Comparable { String name; int age; Employee(String name, int age) { this.name = name; this.age = age; } //overridden compareTo method @Override public int compareTo(Object o) { return this.age - ((Employee) o).age; } } public class ComparableDemo { public static void main(String[] args) { // CREATION List list = new ArrayList<>(); //INSERTION list.add(new Employee("Krishna", 30)); list.add(new Employee("Archana", 28)); list.add(new Employee("Vineet", 25)); list.add(new Employee("Ramesh", 38)); list.add(new Employee("Alok", 28)); System.out.println("Before sorting: "); for (Employee e : list) { System.out.print("[ EMP : age = " + e.age + " ] "); } //SORTING Collections.sort(list); System.out.println("After sorting: "); for (Employee e : list) { System.out.print("[ EMP : age = " + e.age + " ] "); } } }
Before sorting: [ EMP : age = 2 ] [ EMP : age = 33 ] [ EMP : age = 11 ] [ EMP : age = 34 ] [ EMP : age = 7 ] After sorting: [ EMP : age = 2 ] [ EMP : age = 7 ] [ EMP : age = 11 ] [ EMP : age = 33 ] [ EMP : age = 34 ]
compare() method compares the first object with the second object and returns an integer
public int compare (Object o1,Object o2)
import java.util.*; class Student { String name; int age, roll; Student(String name, int age, int roll) { this.name = name; this.age = age; this.roll = roll; } } class AgeComparator implements Comparator { @Override public int compare(Object o1, Object o2) { return ((Student) o1).age - ((Student) o2).age; } } class RollComparator implements Comparator { @Override public int compare(Object o1, Object o2) { return ((Student) o1).roll - ((Student) o2).roll; } } public class ComparatorDemo { public static void main(String[] args) { List list = new ArrayList<>(); list.add(new Student("Ramesh", 30, 20)); list.add(new Student("Adithya", 7, 10)); list.add(new Student("Krishna", 25, 5)); list.add(new Student("Vineet", 24, 15)); System.out.println("BEFORE SORTING"); for (Student e : list) { System.out.println("[ STU : name = " + e.name + " age = " + e.age + " roll = " + e.roll + "]"); } Collections.sort(list,new AgeComparator()); System.out.println("AFTER SORTING WITH AGE"); for (Student e : list) { System.out.println("[ STU : name = " + e.name + " age = " + e.age + " ]"); } Collections.sort(list,new RollComparator()); System.out.println("AFTER SORTING WITH ROLL"); for (Student e : list) { System.out.println("[ STU : name = " + e.name + " roll = " + e.roll + " ]"); } } }
BEFORE SORTING [ STU : name = Ramesh age = 30 roll = 20 ] [ STU : name = Adithya age = 7 roll = 10 ] [ STU : name = Krishna age = 25 roll = 5 ] [ STU : name = Vineet age = 24 roll = 15 ] AFTER SORTING WITH AGE [ STU : name = Adithya age = 7 ] [ STU : name = Vineet age = 24 ] [ STU : name = Krishna age = 25 ] [ STU : name = Ramesh age = 30 ] AFTER SORTING WITH ROLL [ STU : name = Krishna roll = 5 ] [ STU : name = Adithya roll = 10 ] [ STU : name = Vineet roll = 15 ] [ STU : name = Ramesh roll = 20 ]