Master java skills

Sorting ArrayList using Comparator

There is an issue with usage of Comparable interface. It can support only one sorting logic at a time. If we want to sort an array list using different sorting logic, then it is not possible with Comparable interface.

Java has provided a solution to overcome this problem. This can be done using Comparator interface. We can have multiple Comparator objects with multiple sorting logic. These comparator objects can be used to sort the list in different ways. For this purpose, we need to use an overloaded version of Collections.sort() method. Below is an example

//Here NameComparator is a custom class which we have to write
NameComparator nameComparator = new NameComparator();
Collections.sort(arrayList, nameComparator);

In the below example, we are creating two comparator classes of Cricketer type. In one of them, we are sorting based on cricketer name; whereas in the second one, we are sorting based on cricketer id.

Cricketer Class

package com.javatrainingschool;

public class Cricketer {

	private int id;
	private String name;
	
	public Cricketer(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

        //getter and setter methods
}

Name comparator class that sorts based on Cricketer name

package com.javatrainingschool;

import java.util.Comparator;

public class NameComparator implements Comparator<Cricketer>{

	@Override
	public int compare(Cricketer c1, Cricketer c2) {
		return c1.getName().compareTo(c2.getName());
	}
}

ID comparator class that sorts based on Cricketer id

package com.javatrainingschool;

import java.util.Comparator;

public class IDComparator implements Comparator<Cricketer>{

	@Override
	public int compare(Cricketer c1, Cricketer c2) {
		
		int result = 0;
		result = Integer.valueOf(c1.getId()).compareTo(c2.getId());
		return result;
	}

}

Main class that has main method

package com.javatrainingschool;

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

public class ArrayListSortingExample {

	public static void main(String[] args) {
		
		List<Cricketer> cricketerList = new ArrayList<Cricketer>();
		
		Cricketer c1 = new Cricketer(2, "Sunil Gavaskar");
		Cricketer c2 = new Cricketer(3, "Viv Richards");
		Cricketer c3 = new Cricketer(1, "Ricky Ponting");
		Cricketer c4 = new Cricketer(4, "Sachin Tendulkar");
		
		cricketerList.add(c1);
		cricketerList.add(c2);
		cricketerList.add(c3);
		cricketerList.add(c4);
		
		System.out.println("List before sorting : " + cricketerList);
		
                NameComparator nameComparator = new NameComparator();
		Collections.sort(cricketerList, nameComparator);
		System.out.println("List after sorting on name : " + cricketerList);
		
                Collections.sort(cricketerList, new IDComparator());
		System.out.println("List after sorting on id : " + cricketerList);
	}
}
Output :
List before sorting : [Cricketer [id=2, name=Sunil Gavaskar], Cricketer [id=3, name=Viv Richards], Cricketer [id=1, name=Ricky Ponting], Cricketer [id=4, name=Sachin Tendulkar]]

List after sorting on name : [Cricketer [id=1, name=Ricky Ponting], Cricketer [id=4, name=Sachin Tendulkar], Cricketer [id=2, name=Sunil Gavaskar], Cricketer [id=3, name=Viv Richards]]

List after sorting on id : [Cricketer [id=1, name=Ricky Ponting], Cricketer [id=2, name=Sunil Gavaskar], Cricketer [id=3, name=Viv Richards], Cricketer [id=4, name=Sachin Tendulkar]]