Skip to content

Comparable vs Comparator

yuifuku1118 edited this page Nov 27, 2020 · 2 revisions

Java provides two interfaces to sort objects using data members of the class: 1. Comparable 2. Comparator

But what is the difference?

Using Comparable Interface

Comparable interface supports a single sorting sequence which means that it compares elements based on a single elements. As well as that, using Comparable interface will affect your original object class since you need to override compareTo() method inside the class.

Using Comparator Interface

Unlike Comparable, Comparator interface supports multiple sorting sequences which means that it can compare elements based on multiple elements. As well as that, using Comparator interface will not affect your original class, since you need to create Comparator classes and override compare() method inside the comparator class.

For example, let's have a simple student class that has a name and GPA as attributes.

class Student{
	String name;
	double gpa;
	
	public Student(String name, double cap){
		this.name = name;
		this.gpa = cap;
	}
	public String toString(){
		return this.name + " GPA of " + this.gpa;
	}
	public String getName(){ return this.name; }
	public double getGPA(){ return this.gpa; }
}

Using Comparable interface we need to implement Comparable interface and override the compareTo() method. Lets implement Comparable interface and override compareTo() method so that it can be sorted by descending order of GPA.

class Student implements Comparable<Student>{
	String name;
	double gpa;
	
	public Student(String name, double cap){
		this.name = name;
		this.gpa = cap;
	}
	public String toString(){
		return this.name + " GPA of " + this.gpa;
	}
	
	@Override
	public int compareTo(Student other) {
		if (this.getGPA() < other.getGPA()) return 1; 
		if (this.getGPA() > other.getGPA()) return -1; 
		else return 0; 
	}
	
	public String getName(){ return this.name; }
	public double getGPA(){ return this.gpa; }
}

Now we have done this, we can sort students by their GAP using Collection.sort(List students) Lets have driver class to test it out.

public class Compareman{
	public static void main(String[] args){
		ArrayList<Student> students = new ArrayList<Student>(); 
		
		students.add(new Student("Brock", 2.1));
		students.add(new Student("Josh", 1.1));
		students.add(new Student("Bryan", 4.1));
		students.add(new Student("James", 3.1));
		
		Collections.sort(students); 
		
		System.out.println("Students sorted by GPA: "); 
		for(Student s : students){
			System.out.println(s);
		}
	}
}

Doing this we will get following result

Students sorted by GPA:
Bryan GPA of 4.1
James GPA of 3.1
Brock GPA of 2.1
Josh GPA of 1.1

Now what if we want to compare students by multiple elements such as Name and GPA?

Now lets use Comparator interface to see how we can achieve that. Using Comparator interface, we need to create separate Comparator object class for each element that you want to compare and override the compare() method inside each class however we do not need to modify our original Student class.

class Student{
	String name;
	double gpa;
	
	public Student(String name, double cap){
		this.name = name;
		this.gpa = cap;
	}
	public String toString(){
		return this.name + " GPA of " + this.gpa;
	}
	public String getName(){ return this.name; }
	public double getGPA(){ return this.gpa; }
}

class GpaComparator implements Comparator<Student> { 
	public int compare(Student s1, Student s2) { 
		if (s1.getGPA() < s2.getGPA()) return 1; 
		if (s1.getGPA() > s2.getGPA()) return -1; 
		else return 0; 
	} 
} 

class NameComparator implements Comparator<Student> { 
	public int compare(Student s1, Student s2) { 
		return s1.getName().compareTo(s2.getName()); 
	} 
} 

Now we have Comparator object classes for each GAP and name, we can compare for both of them in out driving class using Collection.sort(List students, Comparator comparator)

public class Compareman{
	public static void main(String[] args){
		ArrayList<Student> students = new ArrayList<Student>(); 
		
		students.add(new Student("Josh", 1.1));
		students.add(new Student("Brock", 2.1));
		students.add(new Student("James", 3.1));
		students.add(new Student("Bryan", 4.1));
		
		System.out.println("Students sorted by name: "); 
		NameComparator compareByName = new NameComparator();
		Collections.sort(students, compareByName);
		for(Student s : students){
			System.out.println(s);
		}
		
		System.out.println("Students sorted by GPA: "); 
		GpaComparator compareByGpa = new GpaComparator();
		Collections.sort(students, compareByGpa);
		for(Student s : students){
			System.out.println(s);
		}
	}
}

Doing this will get following result

Students sorted by name:
Brock GPA of 2.1
Bryan GPA of 4.1
James GPA of 3.1
Josh GPA of 1.1

Students sorted by GPA:
Bryan GPA of 4.1
James GPA of 3.1
Brock GPA of 2.1
Josh GPA of 1.1

Clone this wiki locally