Table of Contents
In this post, we will see how you can use comparable to sort list of objects in java.
Comparable interface:
Class whose objects to be sorted must implement this interface.In this,we have to implement compareTo(Object) method.
For example:
1 2 3 4 5 6 7 |
public class Country implements Comparable{      @Override    public int compareTo(Country country) {       return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ; }} |
If any class implements comparable inteface then collection of that object can be sorted automatically using Collection.sort() or Arrays.sort().Object will be sort on the basis of compareTo method in that class.
Objects which implement Comparable in java can be used as keys in a SortedMap like TreeMap or SortedSet like TreeSet without implementing any other interface.
Sorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objects
Java code:
For Comparable:
We will create class country having attribute id and name.This class will implement Comparable interface and implement CompareTo method to sort collection of country object by id.
1. Country.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package org.arpit.java2blog; //If this.cuntryId < country.countryId:then compare method will return -1 //If this.countryId > country.countryId:then compare method will return 1 //If this.countryId==country.countryId:then compare method will return 0 public class Country implements Comparable{    int countryId;    String countryName;        public Country(int countryId, String countryName) {       super();       this.countryId = countryId;       this.countryName = countryName;    }    @Override    public int compareTo(Country country) {      return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ;    }    public int getCountryId() {       return countryId;    }    public void setCountryId(int countryId) {       this.countryId = countryId;    }    public String getCountryName() {       return countryName;    }    public void setCountryName(String countryName) {       this.countryName = countryName;    }   } |
2.ComparableMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ComparableMain { /** * @author Arpit Mandliya */ public static void main(String[] args) { Country indiaCountry=new Country(1, "India"); Country chinaCountry=new Country(4, "China"); Country nepalCountry=new Country(3, "Nepal"); Country bhutanCountry=new Country(2, "Bhutan"); List listOfCountries = new ArrayList(); listOfCountries.add(indiaCountry); listOfCountries.add(chinaCountry); listOfCountries.add(nepalCountry); listOfCountries.add(bhutanCountry); System.out.println("Before Sort : "); for (int i = 0; i < listOfCountries.size(); i++) { Country country=(Country) listOfCountries.get(i); System.out.println("Country Id: "+country.getCountryId()+"||"+"Country name: "+country.getCountryName()); } Collections.sort(listOfCountries); System.out.println("After Sort : "); for (int i = 0; i < listOfCountries.size(); i++) { Country country=(Country) listOfCountries.get(i); System.out.println("Country Id: "+country.getCountryId()+"|| "+"Country name: "+country.getCountryName()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
Before Sort : Country Id: 1||Country name: India Country Id: 4||Country name: China Country Id: 3||Country name: Nepal Country Id: 2||Country name: Bhutan After Sort : Country Id: 1|| Country name: India Country Id: 2|| Country name: Bhutan Country Id: 3|| Country name: Nepal Country Id: 4|| Country name: China |