Java Sorting using Comparable interface

Today we will talk more about java sorting mechanisms. If you want to do sorting of objects of your types, one of the solutions is implementing Comparable interface. In our example we are going to sort a wallet which is full of dollars :) Here is our sample class Dollar class.


* Class represents dollar.
* @author itcuties
public class Dollar implements Comparable<Dollar> {

   // Country eg. US, AUS, CAN
   private String country;
   // Value
   private Integer value;

   // Compare values method
   public int compareTo(Dollar d) {
       // Countries have to match
       // Because we don't take into consideration the the exchange rates
       if (!country.equals(d.getCountry()))
           throw new RuntimeException("Those dollars countries doesn't match");
       // If this obejct's value is greater then d value then a value 1 will be returned
       if (this.value - d.getValue() > 0)
           return 1;
       // If this obejct's value is smaller then d value then a value -1 will be returned
       if (this.value - d.getValue() < 0)
           return - 1;
       // By default they are equal
       return 0;
   public String getCountry() {
       return country;

   public void setCountry(String country) { = country;

   public Integer getValue() {
       return value;

   public void setValue(Integer value) {
       this.value = value;
   public String toString() {
       return getValue()+"$";

This class contains the country where this dollar is used and its value. When you implement the Comparable interface you need to provide an implementation of the compareTo(Dollar d) method. compareTo method returns an integer indicating what is the order of the compared elements, this and the object which is passed through the method argument.
if compareTo returns -1 it means that object from the argument is ‘greater’ than this object.
if compareTo returns 1 it means that object from the argument is ‘smaller’ than this object.
if compareTo returns 0 it means that object from the argument is ‘equal’ to this object.


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

* Test the solution.
* @author itcuties
public class Main {

   public static void main(String[] args) {
       // Dollar values
       int[] values = new int[] {1,5,10,50,100};
       Random random = new Random();
       // Our sample wallet
       List<Dollar> wallet = new ArrayList<Dollar>();
       // Money random generation
       for (int i=0; i<10; i++) {
           Dollar d = new Dollar();
           // Add dollar to the wallet
       // Display unsorted
       for (Dollar d: wallet)
           System.out.print(d + "\t");
       // Sort dollars in our wallet
       // Display sorted
       for (Dollar d: wallet)
           System.out.print(d + "\t");

This is our sample usage. We initialize the wallet variable, which is a List containing Dollar objects, with 10 Dollar objects randomly.
We display the unsorted wallet contents. Next we use Collections.sort() method to sort the wallet contents. At the end sorted wallet contents is displayed.

Download this sample code here.

This code is available on our GitHub repository as well.

One Response to "Java Sorting using Comparable interface"

  1. MD Aftab Alam says:

    it’s a good java tutorial side….. thx for this…


Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>