Home‎ > ‎Applied Core Java‎ > ‎Collection Framework‎ > ‎


Introduction : 

The Comparable interface is used for object natural ordering. Given a collection of objects of the same type, the interface allows you to order the collection into that natural order. It has an Abstract Method compareTo().


The compareTo() method compares the current instance with passed argument.
  • If the current instance comes before the argument in the ordering, a negative value is returned.
  •  If the current instance comes after, then a positive value is returned. 
  • A zero return value just signifies that two objects are ordered at the same position.
 Below is  an Example of Comparable .

Problem :

Create a program that sort Accounts by Account Number using Comparable interface.


Account class will implement java.lang.Comparable interface and define compareTo method from this interface. Method compareTo will compare current object with another account object, that is passed as an argument.  Comparison will be done by Account Number. 

Program :

Step 1 - Coding

(1) Account Class


* Keeps Account information.
* It Implements Comparable Interface
* It has One Abstract Method Comparto which Takes Single Argument .

public class Account implements Comparable<Account> {

public String accountNo;
public double balance;
public Account() {

public Account(String an, double bal) {
accountNo = an;
balance= bal;

public String getAccountNo() {
return accountNo;

public void setAccountNo(String accountNo) {
this.accountNo = accountNo;

public double getBalance() {
return balance;

public void setBalance(double balance) {
this.balance = balance;

* Make use of one implicit object and other explicit object.
* Compare Account Nos. and returns
* -1 : If Account No One is less than Account No Two
* 0 : If both Account Nos. are equal
* 1 : If Account No One is greater than Account No Two
public int compareTo(Account ac) {
    return this.accountNo.compareTo(ac.getAccountNo());

(2) SortAccountListByAccountNo Class


* A program to compare and sort Account objects using Comparable Interface
* implementation.
* A List contains 3 Account objects and sort it by Account Nos. in Ascending order .
public class SortAccountListByAccountNo {

public static void main(String[] args) throws IOException {

ArrayList list = new ArrayList();
list.add(new Account("SBI01", 1000));
list.add(new Account("SBI01", 2000));
list.add(new Account("SBI03", 3000));

//Sort Account No. in ascending order
Iterator<Account> itr = list.iterator();
Account acc = null;
System.out.println("**Account No. List( Ascending )**");
System.out.println(" "+"Account No "+" "+"Balance");

while(itr.hasNext()) {
acc = itr.next();
System.out.println(" "+acc.getAccountNo() + "\t " + acc.getBalance());


Step 2 – Deployment

  1. Create a folder 'c:\sunilos'.
  2. Create or copy Account.java,SortAccountListByAccountNo.java. into 'c:\sunilos' folder.
  3. Open your command prompt and go to 'c:\sunilos' folder
  4. Compile Account.java, SortAccountListbyAccountNo.java with help of
  5. javac *.java command. Command will create class file in the same folder.
  6. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are at command Prompt under c:\sunilos
  2. Now start your java program from command prompt with help of java SortAccountListbyAccountNo command.


**Account Details List( Ascending )**

    Account No Balance

            SBI01 1000.0

            SBI01 2000.0

            SBI03 3000.0


Q 1. What is comparable Interface?

A : The Comparable interface is used to sort collections and arrays of objects using the Collections.sort(obj) and java.utils.Arrays.sort() methods respectively. The objects of the class implementing the Comparable interface can be ordered.

Comparable interface is implemented by all the wrapper classes(String, Float, Double....) and it is called to represent class's natural ordering.

Q 2. When to use Comparable Interface?

A : If the comparison is simpler -- such as when sorting based on only one "key" field, where the other state is irrelevant for the natural ordering -- then the code can be simplified and comparable is used. 

Namrata Sisodiya,
Mar 12, 2011, 5:03 AM
Namrata Sisodiya,
Mar 12, 2011, 5:08 AM