In this article we will see how to write a comparator using Java 8 lambda, and sort a list using Java 8 lambda expressions. We will use "Employee" class list "employees" as example to write comparator and sort it.
class Employee {
	private int id;
	private String name;

	// Getter Setters, constructor and toString()

}
List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));


1) Comparator before Java 8

Comparator<Employee> sortByName = new Comparator<Employee>() {
			@Override
			public int compare(Employee o1, Employee o2) {
				return o1.getName().compareTo(o2.getName());
			}
		};

Comparator<Employee> sortById = new Comparator<Employee>() {
			@Override
			public int compare(Employee o1, Employee o2) {
				return o1.getId() - o2.getId();
			}
		};


2) Comparator in Java 8

Comparator<Employee> sortByName = (Employee o1, Employee o2) -> o1.getName().compareTo(o2.getName()); Comparator<Employee> sortById = (Employee o1, Employee o2) -> o1.getId() - o2.getId();

3) Sorting before Java 8

		List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));

		Comparator<Employee> sortByName = new Comparator<Employee>() {
			@Override
			public int compare(Employee o1, Employee o2) {
				return o1.getName().compareTo(o2.getName());
			}
		};

		System.out.println("Employees before sorting");

		for (Employee employee : employees)
			System.out.println(employee);

		System.out.println("Employees after sorting by name");

		// This will sort employees by name using sortByName constructor
		Collections.sort(employees, sortByName);

		for (Employee employee : employees)
			System.out.println(employee);
Output: Output of above code will look something like this:

Employees before sorting

Employee [id=3, name=Zenny]
Employee [id=1, name=John]
Employee [id=2, name=Ben]

Employees after sorting by name

Employee [id=2, name=Ben]
Employee [id=1, name=John]
Employee [id=3, name=Zenny]


4) Sorting in Java 8 using lambda

		List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));

		System.out.println("Employees before sorting");

		employees.forEach(i -> System.out.println(i));

		System.out.println("Employees after sorting by name");

		// This will sort employees by name 
		employees.sort((Employee o1, Employee o2) -> o1.getName().compareTo(o2.getName()));

		employees.forEach(i -> System.out.println(i));
Output: Output of above code will look something like this:

Employees before sorting

Employee [id=3, name=Zenny]
Employee [id=1, name=John]
Employee [id=2, name=Ben]

Employees after sorting by name

Employee [id=2, name=Ben]
Employee [id=1, name=John]
Employee [id=3, name=Zenny]
  • By Techburps.com
  • Oct 6, 2018
  • Java 8