Sort Employee by Name and Salary in Java 8

In this guide, you will learn how to sort an Employee by his/her name and salary using Java 8.

The Stream API and lambda expression provide a modern and functional approach for processing sequences of elements (e.g., collections). Today, we will dive into a compelling use case of the Stream API: sorting a list of employees by name and salary in both ascending and descending order.

Sorting in Ascending Order

Firstly, we'll define our Employee class with two attributes: name and salary

class Employee {
    private String name;
    private double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    @Override
    public String toString() {
        return "Employee{" + "name='" + name + '\'' + ", salary=" + salary + '}';
    }
}

Now, let’s create a list of Employee objects and sort them in ascending order based on their name first, and then their salary.

import java.util.*;
import java.util.stream.Collectors;

public class AscendingSort {
    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
                new Employee("Vijay Reddy", 30000),
                new Employee("Amit Shah", 60000),
                new Employee("Sara Khan", 50000),
                new Employee("Amit Shah", 40000)
        );

        List<Employee> sortedEmployees = employees.stream()
                .sorted(Comparator.comparing(Employee::getName)
                        .thenComparing(Employee::getSalary))
                .collect(Collectors.toList());

        sortedEmployees.forEach(System.out::println);
    }
}

Output:

Employee{name='Amit Shah', salary=40000.0}
Employee{name='Amit Shah', salary=60000.0}
Employee{name='Sara Khan', salary=50000.0}
Employee{name='Vijay Reddy', salary=30000.0}

Sorting in Descending Order

For descending order, we need to use the reversed() method of Comparator.
public class DescendingSort {
    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
                new Employee("Vijay Reddy", 30000),
                new Employee("Amit Shah", 60000),
                new Employee("Sara Khan", 50000),
                new Employee("Amit Shah", 40000)
        );

        List<Employee> sortedEmployees = employees.stream()
                .sorted(Comparator.comparing(Employee::getName, Comparator.reverseOrder())
                        .thenComparing(Employee::getSalary, Comparator.reverseOrder()))
                .collect(Collectors.toList());

        sortedEmployees.forEach(System.out::println);
    }
}

Output:

Employee{name='Vijay Reddy', salary=30000.0}
Employee{name='Sara Khan', salary=50000.0}
Employee{name='Amit Shah', salary=60000.0}
Employee{name='Amit Shah', salary=40000.0}

In both programs, the sorting logic is encapsulated within a Comparator that is passed to the sorted method of the stream. The Comparator.comparing method is utilized to specify the primary attribute for comparison, while thenComparing is used for the secondary attribute. 

For descending order, we used Comparator.reverseOrder() to reverse the default ascending order. By chaining comparators in such a manner, we achieve a multi-level sort, ensuring our employee data is organized precisely how we desire.

Comments