Saturday, March 5, 2016

Sorting Java objects based on property

In Java, sorting of group of  primitive type is simple. That is, sorting of group of integers, floats, doubles or strings is one step process (if you don't use your own algorithm for sorting).  java.util provides various methods for sorting these kind of group. Two of them are :
  1. Arrays.sort()
  2. Collections.sort()
Why sorting of group of primitive type is simple?

The answer is Java knows what exactly are those types and how to compare those types to each other. For example, comparing two integers is simply using the logical <, = and > operator. Similar for float, double and strings.

But, if you have your custom type (i.e. class) of data, then Java doesn't know what to do with them.  Lets say you have group of people with different properties like height, weight, age, name, religion and so on,  and you want Java to sort these people in increasing order. When Java tries to compare two people, it gets confused. It doesn't know which parameter to consider for sorting. Sorting can be done based on height, weight, age etc but which one to take as a parameter to decide?

To solve this problem, you need to explicitly specify the property. Based on the property, comparison should be carried out i.e. if you want to sort people based on their height, you need to tell Java explicitly that I want to sort people based on their height.

Programatically, this can be accomplished using Comparator interface as follows.

Collections.sort(persons, new Comparator () {
 public int compare(Person p1, Person p2) {
  return, p2.height);

Passing implementation of Comparator interface as a second argument to Collection.sort method helps Java to make decision while comparing between two objects.

In above code snippet, you see that you need to implement one method i.e. compare , to carry out this operation.  If you closely observe the code you can see we  have explicitly specified that sorting needs to be carried out based on height. The compare method returns 3 possible values

  1. A negative integer -> means first argument is less than second argument
  2. Zero -> means both arguments are equal
  3. A positive integer -> means first argument is greater than second argument
A runnable example for sorting is given bellow.

import java.util.*;
class Person {
 // height in cm
 public int height; 
 public String name;
 public Person(int height, String name) {
  this.height = height; = name;
class CustomSort {
 static void sort(List persons) {
  Collections.sort(persons, new Comparator () {
   public int compare(Person p1, Person p2) {
    return, p2.height);
 public static void main(String [] args) {
  List persons = new ArrayList<>();
  // add some persons to arraylist
  persons.add(new Person(154, "John"));
  persons.add(new Person(150, "Henry"));
  persons.add(new Person(160, "Smith"));
  persons.add(new Person(162, "Bob"));
  persons.add(new Person(155, "Rubina"));
  persons.add(new Person(170, "Stiva"));
  persons.add(new Person(178, "Bijay"));
  System.out.println("Sorted persons based on height");
  for (Person person: persons) {

No comments:

Post a Comment