Category: java
Comparator
Published on 18 Feb 2026
Explanation
Comparable is an interface present in the java.lang
package. It is used to define the natural
ordering of objects.
Code:
public interface Comparable<T> {
int compareTo(T obj);
}
Explanation
Comparable is used when you want to sort
objects of a class based on a single
default sorting logic such as id, name, or
salary.
Code:
// Used for default sorting Collections.sort(list);
Explanation
The compareTo() method is used to compare the
current object with another object. It returns negative,
zero, or positive value.
Code:
public int compareTo(Student s) {
return this.id - s.id;
}
Explanation
If compareTo() returns negative, current object is smaller.
If zero, both are equal. If positive, current
object is greater.
Code:
// Negative -> this < other // Zero -> this == other // Positive -> this > other
Explanation
Here is an example where Student class implements
Comparable to sort by id.
Code:
Explanation
Code:
import java.util.*;
class Student implements Comparable<Student> {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
public int compareTo(Student s) {
return this.id - s.id;
}
}
Explanation
Once a class implements Comparable, objects can be
sorted using Collections.sort().
Code:
List<Student> list = new ArrayList<>(); Collections.sort(list);
Explanation
Comparable defines natural ordering. For example, String and
Integer classes already implement Comparable.
Code:
String a = "Apple"; String b = "Banana"; System.out.println(a.compareTo(b));
Explanation
Comparable allows only one sorting logic because compareTo()
method is defined inside the class.
Code:
// Only one default sorting possible
class Student implements Comparable<Student> {
public int compareTo(Student s) {
return this.id - s.id;
}
}
Explanation
Comparable is an interface present in the java.lang
package. It is used to define the natural
ordering of objects.
Code:
public interface Comparable<T> {
int compareTo(T obj);
}
Explanation
Comparable is used when you want to sort
objects of a class based on a single
default sorting logic such as id, name, or
salary.
Code:
// Used for default sorting Collections.sort(list);
Explanation
The compareTo() method is used to compare the
current object with another object. It returns negative,
zero, or positive value.
Code:
public int compareTo(Student s) {
return this.id - s.id;
}
Explanation
If compareTo() returns negative, current object is smaller.
If zero, both are equal. If positive, current
object is greater.
Code:
// Negative -> this < other // Zero -> this == other // Positive -> this > other
Explanation
Here is an example where Student class implements
Comparable to sort by id.
Code:
Explanation
Code:
import java.util.*;
class Student implements Comparable<Student> {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
public int compareTo(Student s) {
return this.id - s.id;
}
}
Explanation
Once a class implements Comparable, objects can be
sorted using Collections.sort().
Code:
List<Student> list = new ArrayList<>(); Collections.sort(list);
Explanation
Comparable defines natural ordering. For example, String and
Integer classes already implement Comparable.
Code:
String a = "Apple"; String b = "Banana"; System.out.println(a.compareTo(b));
Explanation
Comparable allows only one sorting logic because compareTo()
method is defined inside the class.
Code:
// Only one default sorting possible
class Student implements Comparable<Student> {
public int compareTo(Student s) {
return this.id - s.id;
}
}