The 30 second answer
I sort with a comparator on salary, usually descending, then thenComparing on name as a tie-break. For ordered streams that sort is stable. If names or salaries can be null I say so and use nullsLast so the ordering stays well defined.
Could you say this in an interview?
List<Employee> sorted = emps.stream()
.sorted(Comparator.comparingDouble(Employee::salary).reversed()
.thenComparing(Employee::name))
.toList();Ordered-source sort is stable. For nulls: Comparator.nullsLast(...).
Sources (2)
- Comparator.comparingDouble / reversed / thenComparingdocs.oracle.com
- Stream.sorteddocs.oracle.com