Sorting using Comparator in java with example

In this post, we will see Sorting using Comparator in java with example.

Basic points about Comparator interface.

Comparator is an interface available in java.util package, which provides multiple sorting(means we can sort the basis of id and name at the same time).

The Comparator interface contains compare(Object o1, Object o2) method which we can override and keep sorting logic there.

For sorting using Comparator interface we need to use Collections.sort(List list, Comparator c) method.

Different ways of sorting using Comparator interface.

Override compare() method.

@Override
public int compare(Object o1, Object o2) {
Employee e1 = (Employee)o1;
Employee e2 = (Employee)o2;
return e1.getName().compareTo(e2.getName());
}

Using Java 8 lambda expression.

Collections.sort(employeeList, (employee1, employee2) -> employee1.getName().compareTo(employee2.getName()));

 

Using Comparator.comparing().


employeeList.sort(Comparator.comparing(Employee::getId));

 

Example of sorting using Comparator in java using anonymous class.

In this example, we are going to sort Employee type of list on the basis of the name without implementing Comparator(Employee class will not implement Comparator).

 

package hashmap;

import java.util.Collections;
import java.util.*;

class Employee{
	private String name;
	private int id;
	public Employee() {
		
	}
	public Employee(String name, int id) {
		super();
		this.name = name;
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	
}

public class SortingUsingComparator {
public static void main(String[] args)throws Exception {
	
	List<Employee> emplist = new ArrayList<>();
	emplist.add(new Employee("mohan",5));
	emplist.add(new Employee("radhika",1));
	emplist.add(new Employee("gopi",3));
	emplist.add(new Employee("krishna",2));

 System.out.println("sorting using comparator by name ------");
	Collections.sort(emplist, new Comparator() {

		@Override
		public int compare(Object o1, Object o2) {
			Employee e1 = (Employee)o1;
			Employee e2 = (Employee)o2;
			return e1.getName().compareTo(e2.getName());
		}		
		
	});
	
	for(Employee e:emplist) {
		System.out.println(e.getId()+" "+e.getName());
	}
	
	
	System.out.println("sorting using comparator by id  --------");
	Collections.sort(emplist, new Comparator() {

		@Override
		public int compare(Object o1, Object o2) {
			Employee e1 = (Employee)o1;
			Employee e2 = (Employee)o2;
			return e1.getId() -e2.getId();
		}
		
		
	});
	
	for(Employee e:emplist) {
		System.out.println(e.getId()+" "+e.getName());
	}
}
}

Output is –

sorting using comparator by name ——
3 gopi
2 krishna
5 mohan
1 radhika
sorting using comparator by id ——–
1 radhika
2 krishna
3 gopi
5 mohan

 

 

 

 

Example of sorting using Comparator in java implementing Comparator interface.

we can define a separate class which will implement Comparator interface.

 

package collectionsortingwithcomparator;

import java.util.*;
import java.io.*;

class Employee {
	String name;
	int id;

	Employee(String name, int id) {
		this.name = name;
		this.id = id;
	}
}

class NameComparator implements Comparator {
	@Override
	public int compare(Object o1, Object o2) {
		Employee e1 = (Employee) o1;
		Employee e2 = (Employee) o2;
		return e1.name.compareTo(e2.name);
	}
}

class IdComparator implements Comparator {
	@Override
	public int compare(Object o1, Object o2) {
		Employee e1 = (Employee) o1;
		Employee e2 = (Employee) o2;
		return e2.id - e1.id;
	}
}

public class SortByUsingComparator {
	public static void main(String[] args) {
		List<Employee> emplist = new ArrayList<Employee>();
		emplist.add(new Employee("rakesh", 105));
		emplist.add(new Employee("bittu", 103));
		emplist.add(new Employee("tituu", 110));
		emplist.add(new Employee("asharaf", 108));
		System.out.println("sorting by name-------");
		Collections.sort(emplist, new NameComparator());
		Iterator itr = emplist.iterator();
		while (itr.hasNext()) {
			Employee emp = (Employee) itr.next();
			System.out.println(emp.name + " " + emp.id);
		}
		
		
		System.out.println("sorting by id---------");
		Collections.sort(emplist, new IdComparator());
		Iterator itr2 = emplist.iterator();
		while (itr2.hasNext()) {
			Employee emp = (Employee) itr2.next();
			System.out.println(emp.name + " " + emp.id);
		}
	}
}

 

 

The output of the above program is –


anjali 108
bittu 103
rakesh 105
shibha 110
sorting by id———
shibha 110
anjali 108
rakesh 105
bittu 103

 

 

Example of Sorting using Comparator and Java 8.

 

package sortingusingcomparatorjava8;

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

class Employee {
	private String name;
	private int id;

	Employee(String name, int id) {
		this.name = name;
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public int getId() {
		return id;
	}

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

}

public class SortingUsingJava8 {
	public static void main(String[] args) {

		List<Employee> employeeList = new ArrayList<Employee>();
		employeeList.add(new Employee("rakesh", 105));
		employeeList.add(new Employee("bittu", 103));
		employeeList.add(new Employee("shibha", 110));
		employeeList.add(new Employee("anjali", 108));

		// Lambda expression for sorting on basis of name
		System.out.println("sorting on basis of name-------");
		Collections.sort(employeeList, (employee1, employee2) -> employee1.getName().compareTo(employee2.getName()));
		for (Employee employee : employeeList) {
			System.out.println(employee.getName() + " " + employee.getId());
		}

		// Lambda expression for sorting on basis of id
		System.out.println("sorting on basis of id-------");
		Collections.sort(employeeList, (employee1, employee2) -> employee1.getId() - employee2.getId());
		for (Employee employee : employeeList) {
			System.out.println(employee.getName() + " " + employee.getId());
		}

	}
}

The output of above program.

 

sorting on basis of name——-
anjali 108
bittu 103
rakesh 105
shibha 110
sorting on basis of id——-
bittu 103
rakesh 105
anjali 108
shibha 110

 

 

Soring list of an object using Comparator.comparing().

package sortingusingcomparatorjava8;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

class Employee {
	private String name;
	private int id;

	Employee(String name, int id) {
		this.name = name;
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public int getId() {
		return id;
	}

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

}

public class SortingUsingJava8 {
	public static void main(String[] args) {

		List<Employee> employeeList = new ArrayList<Employee>();
		employeeList.add(new Employee("rakesh", 105));
		employeeList.add(new Employee("bittu", 103));
		employeeList.add(new Employee("shibha", 110));
		employeeList.add(new Employee("anjali", 108));

		System.out.println("sorting on basis of name-------");
		employeeList.sort(Comparator.comparing(Employee::getName));
		for (Employee employee : employeeList) {
			System.out.println(employee.getName() + " " + employee.getId());
		}

		System.out.println("sorting on basis of id-------");
		employeeList.sort(Comparator.comparing(Employee::getId));
		for (Employee employee : employeeList) {
			System.out.println(employee.getName() + " " + employee.getId());
		}

	}
}

The output of the above program.

sorting on basis of name——-
anjali 108
bittu 103
rakesh 105
shibha 110
sorting on basis of id——-
bittu 103
rakesh 105
anjali 108
shibha 110

 

That’s all about Sorting using Comparator in java with example.


You may like.

Java Comparator docs.