Comparable vs Comparator in Java

Comparable

  • Comparable provides a single sorting sequence. In other words, we can sort the collection on the basis of a single element such as id, name, and price.
  • Comparable affects the original class (the actual class is modified)
  • Comparable provides compareTo() method to sort elements.
  • Comparable is present on java.lang package.
  • We can sort the list elements of Comparable type by Collections.sort(List) method

Comparator

  • The Comparator provides multiple sorting sequences. In other words, we can sort the collection on the basis of multiple elements such as id, name,` price, etc.
  • Comparator provides compare() method to sort elements.
  • A Comparator is present in java.util package.
  • We can sort the list elements of Comparator type by Collections.sort(List, Comparator) method.

Code explanation

public class Student implements Comparable<Student>{

private int id;
private String name;

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

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

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

@Override
public boolean equals(Object obj){
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Student student = (Student) obj;
return id == student.id && Objects.equals(name, student.name);
}


@Override
public int compareTo(Student student) {
if (id == student.id){
return 0;
}else if (id > student.id){
return 1;
}else{
return -1;
}
}
}
public static void main(String[] args) {

List<Student> studentList = new ArrayList<>();
Student student1 = new Student(327, "Kasun");
Student student2 = new Student(100, "Dasun");
Student student3 = new Student(167, "Thisun");

studentList.add(student1);
studentList.add(student2);
studentList.add(student3);

for(int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i).getId());
}


}
Collections.sort(studentList);
public class IdComparator implements Comparator<Student> {
@Override
public int compare(Student student1, Student student2) {
if (student1.getId() == student2.getId()){
return 0;
}else if (student1.getId() > student2.getId()){
return 1;
} else {
return -1;
}
}
}
public class NameComparator implements Comparator<Student> {
@Override
public int compare(Student student1, Student student2) {
return student1.getName().compareTo(student2.getName());
}
}
Collections.sort(studentList, new NameComparator());orCollections.sort(studentList, new IdComparator());

Interview Question

if (o1.getId() ==  o2.getId()){   return o1.getName().compareTo(o2.getName());}else if (o1.getId() > o2.getId()){   return 1;} else {   return -1;}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kasun Dissanayake

Kasun Dissanayake

Software Engineer at IFS R & D International (Pvt) Ltd || Former Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Mobile