Java Programs›Java 8 Streams›Sort Employees by Salary Using Streams

Sort Employees by Salary Using Streams in Java

intermediate·  Java 8 Streams  ·  Streams

Problem

Comparator.comparing builds a comparator from a key-extractor function, and thenComparing adds a tie-breaker used only when the first key is equal.

Given a list of employees, print them sorted by salary from highest to lowest, ordering employees with equal salaries alphabetically by name.

Input
Asha 85000, Ravi 62000, Meera 92000, John 62000, Liu 58000
Output
Meera, Asha, John, Ravi, Liu

Java Program

Java
import java.util.Arrays; import java.util.Comparator; import java.util.List; public class SortEmployeesStream { static class Employee { private final String name; private final String dept; private final int salary; Employee(String name, String dept, int salary) { this.name = name; this.dept = dept; this.salary = salary; } String getName() { return name; } String getDept() { return dept; } int getSalary() { return salary; } } public static void main(String[] args) { List<Employee> staff = Arrays.asList( new Employee("Asha", "Engineering", 85000), new Employee("Ravi", "Sales", 62000), new Employee("Meera", "Engineering", 92000), new Employee("John", "Sales", 62000), new Employee("Liu", "HR", 58000)); staff.stream() .sorted(Comparator.comparingInt(Employee::getSalary).reversed() // highest salary first .thenComparing(Employee::getName)) // ties broken by name .forEach(e -> System.out.println(e.getName() + " - " + e.getSalary())); } }

Output

Meera - 92000 Asha - 85000 John - 62000 Ravi - 62000 Liu - 58000

Core Logic

A comparator chain describes the ordering declaratively: primary key salary descending, secondary key name ascending.

How It Works
  1. 1Comparator.comparingInt(Employee::getSalary) compares employees by salary, lowest first.
  2. 2.reversed() flips that to highest first.
  3. 3.thenComparing(Employee::getName) is consulted only when two salaries are equal.
  4. 4sorted(...) applies the chain, and forEach prints each employee.
Ravi and John both earn 62000, so the name tie-breaker puts John before Ravi.
💡

Key Point: Order matters in the chain: calling .reversed() after thenComparing would reverse the name order too, putting Ravi before John.

Complexity
Time Complexity: O(n log n)Space Complexity: O(n)

Why: The stream's sort is a comparison sort over n employees, and it buffers all elements to sort them.

Key Concepts

sorted()Comparator.comparingInt()reversed()thenComparing()

Approach 2: Sort by Department, Then Salary

Java
import java.util.Arrays; import java.util.Comparator; import java.util.List; public class SortEmployeesByDeptStream { static class Employee { private final String name; private final String dept; private final int salary; Employee(String name, String dept, int salary) { this.name = name; this.dept = dept; this.salary = salary; } String getName() { return name; } String getDept() { return dept; } int getSalary() { return salary; } } public static void main(String[] args) { List<Employee> staff = Arrays.asList( new Employee("Asha", "Engineering", 85000), new Employee("Ravi", "Sales", 62000), new Employee("Meera", "Engineering", 92000), new Employee("John", "Sales", 62000), new Employee("Liu", "HR", 58000)); staff.stream() .sorted(Comparator.comparing(Employee::getDept) // A to Z by department .thenComparing(Employee::getSalary, Comparator.reverseOrder())) // then richest first .forEach(e -> System.out.println(e.getDept() + ": " + e.getName() + " - " + e.getSalary())); } }

Output

Engineering: Meera - 92000 Engineering: Asha - 85000 HR: Liu - 58000 Sales: Ravi - 62000 Sales: John - 62000

Core Logic

Swapping the keys gives a report-style ordering: departments alphabetically, and within each department the highest salary first.

How It Works
  1. 1Comparator.comparing(Employee::getDept) sorts departments A to Z.
  2. 2thenComparing(Employee::getSalary, Comparator.reverseOrder()) reverses only the salary key.
  3. 3Each line prints department, name and salary.
Engineering comes first, with Meera (92000) above Asha (85000); Sales lists Ravi before John because they tie on salary and the sort keeps their original order.
💡

Key Point: Passing Comparator.reverseOrder() as the second argument reverses just that key — the safe way to mix ascending and descending fields.

Complexity
Time Complexity: O(n log n)Space Complexity: O(n)

Why: Same comparison sort, with a different comparator.

Key Concepts

Comparator.comparing()Comparator.reverseOrder()multi-level sort

Interview Questions

Does sorted() change the original list?

No. A stream never modifies its source; the sorted result exists only in the stream or the new collection it's collected into.

How do you get only the top three earners?

Add .limit(3) after sorted(...) in the pipeline.

Learn the Concept

Related Programs