Categories
Java

The Comparable interface

A List contains objects that you can order according to a natural order using the static method Collections.sort(List list) if the objects implement the Comparable interface.
The only method required by the Comparable interface is compareTo which has as argument an object of the same type you are sorting and the return value is an integer.
So the call to the method is:
int result = x.compareTo(y)
where x and y are objects of the same type of those in the list and result is an integer that takes the value:

  • <0 if x<y
  • ==0 if x==y
  • >0 if x>y

Look the follow example:

  • Main.java
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class Main {
    
        public static void main(String[] args) {
    
    	// FIRST SECTION
    
    	List<String> myStringList = new ArrayList<String>();
    
    	myStringList.add(new String("Germany"));
    	myStringList.add(new String("French"));
    	myStringList.add(new String("United Kingdom"));
    	myStringList.add(new String("Italy"));
    	myStringList.add(new String("Spain"));
    
    	System.out.println("UNSORTED LIST:");
    	for (String item : myStringList) {
    	    System.out.println(item);
    	}
    
    	// SECOND SECTION
    
    	System.out.println("\nSORTED LIST:");
    	Collections.sort(myStringList);
    
    	for (String item : myStringList) {
    	    System.out.println(item);
    	}
    
    	// THIRD SECTION
    
    	List<Country> myCountryList = new ArrayList<Country>();
    
    	Country germany = new Country("Germany", 357021);
    	Country french = new Country("French", 547030);
    	Country unitedKingdom = new Country("United Kingdom", 244820);
    	Country italy = new Country("Italy", 301230);
    	Country spain = new Country("Spain", 504851);
    
    	myCountryList.add(germany);
    	myCountryList.add(french);
    	myCountryList.add(unitedKingdom);
    	myCountryList.add(italy);
    	myCountryList.add(spain);
    
    	System.out.println("\nSORTED LIST BY AREA:");
    	Collections.sort(myCountryList);
    
    	for (Country item : myCountryList) {
    	    System.out.println(item.getName());
    	}
        }
    }
    
  • Country.java
    public class Country implements Comparable<Country> {
    
        private String name;
        private int area;
    
        public Country(String name, int area) {
    	this.name = name;
    	this.area = area;
        }
    
        public String getName() {
    	return name;
        }
    
        public void setName(String name) {
    	this.name = name;
        }
    
        public int getArea() {
    	return area;
        }
    
        public void setArea(int area) {
    	this.area = area;
        }
    
        @Override
        public int compareTo(Country o) {
    	int result;
    	result = area - o.getArea();
    	return result;
        }
    }
    

The output is as follows:

UNSORTED LIST:
Germany
French
United Kingdom
Italy
Spain

SORTED LIST:
French
Germany
Italy
Spain
United Kingdom

SORTED LIST BY AREA:
United Kingdom
Italy
Germany
Spain
French

In the first section the list is printed in the order in which the elements have been added to the list.
In the second section you can sort with Collections.sort because the String class implements the Comparable interface and the order is alphabetic.
In the third section, the list contains objects (Country) that implement Comparable and then the sort is ascending by area because of I implemented the compareTo method.

Similarly, you can also sort the arrays using the Arrays.sort(Object[] a) method.

One reply on “The Comparable interface”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.