Comparator in java with Example

Comparator is an Interface in java which is present in java.util package.

Comparator interface has only method comapare which has two argument.

1
2
3
4
5
6
7
 
 
public int compare(Object o1, Object o2){
 
//Logic to compare two object
 
}

A comparison function, which imposes a total ordering on some collection of objects.

Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don’t have a natural
ordering
.

The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S.

Let’s Understand using Example.

Sorting ArrayList element based on last digit of element.

 
SortNumber.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package Comaprator;
 
import java.util.Comparator;
 
public class SortNumber implements Comparator<Integer>
{
 
	@Override
	public int compare(Integer o1, Integer o2) {
 
		if(o1%10>o2%10)
			return 1;
 
		return -1;
	}
}

Comaprator.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
package Comaprator;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
 
public class Comaprator
{
	public static void main(String[] args) 
	{
 
		//Integer arraylist
 
		ArrayList<Integer> list=new ArrayList<Integer>();
 
		list.add(533);
		list.add(224);
		list.add(111);
		list.add(325);
		list.add(495);
 
		Comparator<Integer> comp=new SortNumber();
 
		Collections.sort(list, comp);
 
		for (Integer integer : list) 
		{
 
			System.out.println(integer);
 
		}
 
	}
}

Output:
111
533
224
495
325

Sorting ArrayList element using Comparable

How to sort User define object ?


Let’s say we have Employee Object and we want sort based on Employee id Then we have to use Comparable Interface of java.util package.

Comparable Interface


This interface imposes a total ordering on the objects of each class that implements it.
This ordering is referred to as the class’s natural ordering, and the class’s compareTo method is referred to as its natural comparison method.

Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort).

Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.

The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C.

Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.

Sorting Arraylist Object

Employee.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
41
42
43
44
45
46
47
48
49
50
51
package com.spy.Employee.Arraylist;
 
public class Employee implements Comparable<Employee>
{
 
	int id;
	String name;
 
	public Employee(int id,String name) 
	{
 
		this.id=id;
		this.name=name;
 
	}
	public int getId() 
	{
		return id;
	}
 
	public void setId(int id) 
	{
		this.id = id;
	}
 
	public String getName() 
	{
		return name;
	}
 
	public void setName(String name) 
	{
		this.name = name;
	}
 
	@Override
	public int compareTo(Employee e) 
	{
 
		int emp_id =((Employee)e).getId();
 
		return this.id-emp_id;
	}
 
	@Override
	public String toString() 
	{
		return "Employee [id=" + id + ", name=" + name + "]";
	}
 
}

ArrayList_emp.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
package com.spy.Employee.Arraylist;
 
import java.util.ArrayList;
import java.util.Collections;
 
public class ArrayList_emp 
{
 
	public static void main(String[] args) 
	{
 
 
		ArrayList<Employee> emp_list=new ArrayList<Employee>();
 
 
		emp_list.add(new Employee(2,"Bhavesh"));
		emp_list.add(new Employee(3,"Sahil"));
		emp_list.add(new Employee(1,"Nilay"));
 
 
		Collections.sort(emp_list);
		for (Employee emp : emp_list) 
		{
 
			System.out.println("Employee id: "+emp.getId());
			System.out.println("Employee name: "+emp.getName());
			System.out.println("**********************");
 
		}	
	}	
}

Output: