by BehindJava

Write a Comparator in Java to compare two employees based upon name, age and address

Home » java » Write a Comparator in Java to compare two employees based upon name, age and address

In this tutorial we are going to learn about Comparator in Java i.e., to compare the list of employees and sort them not just based on the single attribute that is ID which is a natural sorting, Instead use different comparators like name, age and address.

In order to sort Employee object on different criteria, we need to create multiple comparators. example: NameComparator, AgeComparator and AddressComparator and this is known as custom sorting in Java.

This is different from the natural ordering of object, provided by the compareTo() method of java.lang,Comparable interface.

Compare() method returns

  1. Negative if 1st parameter is < 2nd parameter.
  2. Positive is 1st parameter is > 2nd parameter.
  3. O if 1st parameter is = 2nd parameter.

Now, we will create a custom object employee who’s list is required for further use, with id, age, name and address.

package pract;

public class Employees {
	private int id;
	private int age;
	private String name;
	private Address address;

	public Employees(int id, int age, String name, Address address) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
		this.address = address;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Employees [id=" + id + ", age=" + age + ", name=" + name + ", address=" + address + "]";
	}

}

Here the sorting with name is easy because String is an in-built class which already has natural sorting into it. So, if you can go into the string class and see, it has implemented the comparable interface and compareTo method.

Here the problem statement is we need to sort the employees based on the address. So lets create the Address class with streetName and pinCode and generate getters and setters.

package pract;

public class Address implements Comparable<Address> {

	private String streetName;
	private int pinCode;

	public Address(String streetName, int pinCode) {
		super();
		this.streetName = streetName;
		this.pinCode = pinCode;
	}

	public String getStreetName() {
		return streetName;
	}

	public void setStreetName(String streetName) {
		this.streetName = streetName;
	}

	public int getPinCode() {
		return pinCode;
	}

	public void setPinCode(int pinCode) {
		this.pinCode = pinCode;
	}

	@Override
	public int compareTo(Address o) {
		return this.getPinCode() - o.pinCode;
	}

	@Override
	public String toString() {
		return "Address [streetName=" + streetName + ", pinCode=" + pinCode + "]";
	}

}

If we are going to implement a comparator for address. we need to compare two addresses and lets say we are going to sort the employees based on the pin code of the addresses like 23, 33, 43 and we are going to sort them by implementing Comparable interface where we pass the object of Address class to compareTo method for comparison.

package pract;

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

public class BehindJavaMain {
	public static void main(String args[]) {
		Employees e1 = new Employees(1, 22, "Ram", new Address("Baker St", 223));
		Employees e2 = new Employees(2, 19, "Sam", new Address("Caker St", 224));
		Employees e3 = new Employees(3, 24, "Sai", new Address("Saker St", 223));
		Employees e4 = new Employees(4, 21, "Raj", new Address("Daker St", 225));
		Employees e5 = new Employees(5, 26, "Om", new Address("Faker St", 226));
		Employees e6 = new Employees(6, 23, "Hari", new Address("Eaker St", 221));
	
		List<Employees> empList = new ArrayList<>();
		empList.add(e1);
		empList.add(e2);
		empList.add(e3);
		empList.add(e4);
		empList.add(e5);
		empList.add(e6);
		
		System.out.println(empList);
		System.out.println("******************************************");
		Collections.sort(empList, new AgeComparator());
		System.out.println("Sorting based on the age:"+empList);
		System.out.println("******************************************");
		Collections.sort(empList, new NameComparator());
		System.out.println("Sorting based on the name:"+empList);
		System.out.println("******************************************");
		Collections.sort(empList, new AddressComparator());
		System.out.println("Sorting based on the address:"+empList);
		System.out.println("******************************************");
	}

}

Now, We are going to work on implementing the AgeComparator, NameComparator and the AddressComparator.

package pract;

import java.util.Comparator;

public class AgeComparator implements Comparator<Employees> {

	@Override
	public int compare(Employees o1,Employees o2) {
		return o1.getAge() - o2.getAge();
	}

}
package pract;

import java.util.Comparator;

public class NameComparator implements Comparator<Employees> {

	@Override
	public int compare(Employees o1, Employees o2) {
		return o1.getName().compareTo(o2.getName());
	}

}
package pract;

import java.util.Comparator;

public class AddressComparator implements Comparator<Employees> {

	@Override
	public int compare(Employees o1, Employees o2) {
		return o1.getAddress().compareTo(o2.getAddress());
	}

}

Output based on the above comparators.

[Employees [id=1, age=22, name=Ram, address=Address [streetName=Baker St, pinCode=223]], Employees [id=2, age=19, name=Sam, address=Address [streetName=Caker St, pinCode=224]], Employees [id=3, age=24, name=Sai, address=Address [streetName=Saker St, pinCode=223]], Employees [id=4, age=21, name=Raj, address=Address [streetName=Daker St, pinCode=225]], Employees [id=5, age=26, name=Om, address=Address [streetName=Faker St, pinCode=226]], Employees [id=6, age=23, name=Hari, address=Address [streetName=Eaker St, pinCode=221]]]
******************************************
Sorting based on the age:[Employees [id=2, age=19, name=Sam, address=Address [streetName=Caker St, pinCode=224]], Employees [id=4, age=21, name=Raj, address=Address [streetName=Daker St, pinCode=225]], Employees [id=1, age=22, name=Ram, address=Address [streetName=Baker St, pinCode=223]], Employees [id=6, age=23, name=Hari, address=Address [streetName=Eaker St, pinCode=221]], Employees [id=3, age=24, name=Sai, address=Address [streetName=Saker St, pinCode=223]], Employees [id=5, age=26, name=Om, address=Address [streetName=Faker St, pinCode=226]]]
******************************************
Sorting based on the name:[Employees [id=6, age=23, name=Hari, address=Address [streetName=Eaker St, pinCode=221]], Employees [id=5, age=26, name=Om, address=Address [streetName=Faker St, pinCode=226]], Employees [id=4, age=21, name=Raj, address=Address [streetName=Daker St, pinCode=225]], Employees [id=1, age=22, name=Ram, address=Address [streetName=Baker St, pinCode=223]], Employees [id=3, age=24, name=Sai, address=Address [streetName=Saker St, pinCode=223]], Employees [id=2, age=19, name=Sam, address=Address [streetName=Caker St, pinCode=224]]]
******************************************
Sorting based on the address:[Employees [id=6, age=23, name=Hari, address=Address [streetName=Eaker St, pinCode=221]], Employees [id=1, age=22, name=Ram, address=Address [streetName=Baker St, pinCode=223]], Employees [id=3, age=24, name=Sai, address=Address [streetName=Saker St, pinCode=223]], Employees [id=2, age=19, name=Sam, address=Address [streetName=Caker St, pinCode=224]], Employees [id=4, age=21, name=Raj, address=Address [streetName=Daker St, pinCode=225]], Employees [id=5, age=26, name=Om, address=Address [streetName=Faker St, pinCode=226]]]
******************************************