record Fio(String name, String surname) {};
record Worker(int id, Fio fio, Integer salary, LocalDateTime hiredOn) {};
class WorkerComparator implements Comparator<Worker> {
private static final Comparator safeCompare = Comparator.nullsFirst(Comparator.naturalOrder());
@Override
public int compare(Worker o1, Worker o2) {
return Objects.compare(o1, o2,
Comparator.comparing(key -> ((Worker)key).fio().surname(), safeCompare)
.thenComparing(key -> ((Worker)key).fio().name(), safeCompare)
.thenComparing(key -> ((Worker)key).salary(), safeCompare)
.thenComparing(key -> ((Worker)key).hiredOn(), safeCompare));
}
}
((Worker)key)
return Objects.compare(o1, o2,
Comparator.<Worker, String>comparing(key -> key.fio().surname(), safeCompare)
.<String>thenComparing(key -> ((Worker)key).fio().name(), safeCompare)
.<Integer>thenComparing(key -> ((Worker)key).salary(), safeCompare)
.<LocalDateTime>thenComparing(key -> ((Worker)key).hiredOn(), safeCompare));
default <U> Comparator<T> thenComparing(
Function<? super T, ? extends U> keyExtractor,
Comparator<? super U> keyComparator)
{
return thenComparing(comparing(keyExtractor, keyComparator));
}
return Objects.compare(o1, o2,
Comparator.<Worker, String>comparing(key -> key.fio().surname())
.thenComparing(key -> key.fio().name())
.thenComparing(Worker::salary)
.thenComparing(Worker::hiredOn));
class WorkerComparator implements Comparator<Worker> {
final static Comparator<String> safeString = Comparator.nullsFirst(Comparator.naturalOrder());
final static Comparator<Integer> safeInteger = Comparator.nullsFirst(Comparator.naturalOrder());
final static Comparator<LocalDateTime> safeLocalDateTime = Comparator.nullsFirst(Comparator.naturalOrder());
@Override
public int compare(Worker o1, Worker o2) {
return Objects.compare(o1, o2,
Comparator.<Worker, String>comparing(key -> key.fio().surname(), safeString)
.thenComparing(key -> key.fio().name(), safeString)
.thenComparing(Worker::salary, safeInteger)
.thenComparing(Worker::hiredOn, safeLocalDateTime));
}
}
public static void main(String[] args) {
Comparator<Worker> c1 = Comparator.comparing(key -> key.fio().surname(), Comparator.nullsFirst(Comparator.naturalOrder()));
Comparator<Worker> c2 = Comparator.comparing(key -> key.fio().name(),Comparator.nullsFirst(Comparator.naturalOrder()));
Comparator<Worker> c3 = Comparator.comparing(Worker::salary, Comparator.nullsFirst(Comparator.naturalOrder()));
Comparator<Worker> c4 = Comparator.comparing(Worker::hiredOn, Comparator.nullsFirst(Comparator.naturalOrder()));
List<Worker> workers = new ArrayList<>(List.of(
new Worker(1, new Fio(null, null), 50, null),
new Worker(1, new Fio(null, null), 15, null),
new Worker(1, new Fio("Bob",null ), 15, null)
));
workers.sort(c1.thenComparing(c2).thenComparing(c3).thenComparing(c4));
workers.forEach(System.out::println);
}
public static void main(String[] args) {
Comparator<Fio> cFio1 = Comparator.comparing(Fio::name, Comparator.nullsFirst(String::compareTo));
Comparator<Fio> cFio2 = Comparator.comparing(Fio::surname,Comparator.nullsFirst(String::compareTo));
Comparator<Worker> cFioCommon = Comparator.comparing(Worker::fio,Comparator.nullsFirst(cFio1).thenComparing(Comparator.nullsFirst(cFio2)));
Comparator<Worker> c3 = Comparator.comparing(Worker::salary, Comparator.nullsFirst(Comparator.naturalOrder()));
Comparator<Worker> c4 = Comparator.comparing(Worker::hiredOn, Comparator.nullsFirst(Comparator.naturalOrder()));
List<Worker> workers = new ArrayList<>(List.of(
new Worker(1, new Fio(null, null), 50, null),
new Worker(1,null, 50, null),
new Worker(1, new Fio("Bob", null), 15, null),
new Worker(1, new Fio("Alice",null ), 15, null)
));
workers.add(null);
workers.sort(Comparator.nullsFirst(cFioCommon.thenComparing(c3).thenComparing(c4)));
workers.forEach(System.out::println);
}