Код: Выделить всё
import java.util.Comparator;
public class Employee {
public static class EmployeeNameComparator implements Comparator {
@Override
public int compare(Employee o1, Employee o2) {
return o1.name.compareTo(o2.name);
}
}
private String name;
private int id;
private int yearStarted;
public Employee(String name, int id, int yearStarted) {
this.name = name;
this.id = id;
this.yearStarted = yearStarted;
}
@Override
public String toString() {
return "%d %-15s %d".formatted(id, name, yearStarted);
}
}
Код: Выделить всё
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List employeeList = new ArrayList(List.of(
new Employee("Jack", 1002, 2014),
new Employee("Mary", 1031, 2019),
new Employee("Andrew", 1982, 2024),
new Employee("Bob", 1721, 2000)
));
employeeList.sort(new Employee.EmployeeNameComparator());
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... ss-in-java
Мобильная версия