What is the Java Comparable interface?

Java Comparable interface is used to compare objects and sort them according to the natural order.

Natural ordering is referred to as its compareTo() function. The string objects are sorted lexicographically, and the wrapper class objects are sorted according to their built-in compareTo() function (like how Integers objects are sorted in ascending order).


Public int compareTo(obj)

The compareTo() function compares the current object with the provided object. This function is already implemented for default wrapper classes and primitive data types; but, this function also​ needs to be implemented for user-defined classes.

It returns:

  1. a positive integer, if the current object is greater than the provided object.

  2. a negative integer, if the current object is less than the provided object.

  3. zero, if the current object is equal to the provided object.


Sorting according to the natural order

svg viewer

Once the compareTo() function is implemented in the class, either Collections.sort() or Arrays.sort() is called for sorting in the driver function.

Collection.sort() vs Array.sort()

1.Collection.sort() is used to sort a list of objects.

2.Array.sort() is used to sort an array of objects.

svg viewer

Examples

1. Sorting strings using Collections.sort():

// Using Collections.sort() to sort list.
import java.util.*;
public class Collectionsort
{
public static void main(String[] arguments)
{
ArrayList<String> list = new ArrayList<String>();
list.add("E");
list.add("D");
list.add("U");
list.add("C");
list.add("A");
list.add("T");
list.add("I");
list.add("V");
list.add("E");
//printing list without sorting
System.out.println(list);
//sorting list
Collections.sort(list);
//printing list after sorting
System.out.println(list);
}
}

2. Sorting a list of integers using Collection.sort():

// Using Collections.sort() to sort list.
import java.util.*;
public class Collectionsort
{
public static void main(String[] arguments)
{
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(4);
list.add(0);
list.add(1);
list.add(3);
//printing list without sorting
System.out.println(list);
//sorting
Collections.sort(list);
//printing list after sorting
System.out.println(list);
}
}

3. For user-defined custom objects:

// importing libraries
import java.util.*;
import java.lang.*;
import java.io.*;
class Student implements Comparable <Student>
{
int id;
String name;
//student class
public Student (int id, String name)
{
this.id=id;
this.name=name;
}
//printing function
public String toString()
{
return this.id + " " + this.name;
}
// compareTo() function defined.
// sorting them by their id's
public int compareTo(Student std){
return this.id - std.id;
}
}
// Driver function
class Main
{
public static void main (String[] arguments)
{
ArrayList <Student> list = new ArrayList <Student> ( );
list.add(new Student(2, "Bob"));
list.add(new Student(1, "Aaron"));
list.add(new Student(3, "Carl"));
//printing unsorted
for (int i=0; i<list.size(); i++)
System.out.println(list.get(i));
//sorting.
Collections.sort(list);
//printing after sorting
System.out.println("\n");
for (int i=0; i<list.size(); i++)
System.out.println(list.get(i));
}
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved