Challenge: Sorting Employees
Uppgift
Swipe to start coding
Implement a Comparator
to sort the list of employees
based on the specified criteria.
- Sort the employees by years of experience in ascending order, and then apply the
reversed()
method to make the order descending. - If the years of experience are the same, sort the employees with the same experience by
salary
in descending order. - If both experience and salary are the same, sort by
name
in alphabetical order. - Use the created comparator as an argument for the
sort()
method to sort the employee list. - Print the sorted list of employees.
Lösning
solution
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.example;
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>(Arrays.asList(
new Employee("Michael", 5, 90000, "HR"),
new Employee("Emily", 3, 65000, "IT"),
new Employee("James", 5, 95000, "Marketing"),
new Employee("Sarah", 5, 90000, "IT"),
new Employee("David", 2, 50000, "Finance")
));
// Comparator: Sort by years of experience (descending), then salary (descending), then name (alphabetical)
Comparator<Employee> employeeComparator = Comparator
.comparing(Employee::getYearsOfExperience).reversed()
.thenComparing(Employee::getSalary, Comparator.reverseOrder())
.thenComparing(Employee::getName);
employees.sort(employeeComparator);
employees.forEach(System.out::println);
}
}
class Employee {
private final String name;
private final int yearsOfExperience;
private final double salary;
private final String department;
public Employee(String name, int yearsOfExperience, double salary, String department) {
this.name = name;
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 1. Kapitel 10
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.example;
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>(Arrays.asList(
new Employee("Michael", 5, 90000, "HR"),
new Employee("Emily", 3, 65000, "IT"),
new Employee("James", 5, 95000, "Marketing"),
new Employee("Sarah", 5, 90000, "IT"),
new Employee("David", 2, 50000, "Finance")
));
// Comparator: Sort by years of experience (descending), then salary (descending), then name (alphabetical)
Comparator<Employee> employeeComparator = Comparator
.___(___).___()
.___(___, Comparator.reverseOrder())
.___(___);
employees.sort(___);
employees.forEach(System.out::println);
}
}
class Employee {
private final String name;
private final int yearsOfExperience;
private final double salary;
private final String department;
public Employee(String name, int yearsOfExperience, double salary, String department) {
this.name = name;
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal