I need to sort a List in alphabetical order by name and then to sort it one more time by type and put on the top of the list elements with specific type. This is what I have done so far, but it didn't work as expected and it returns the list sorted only by name.
public List<PRDto> present(
List<ParticipantReference> participants) {
return participants.stream()
.map(MAPPER::toDto)
.sorted(Comparator.comparing(PRDto::getName)
.thenComparing(PRDto::getParticipantType, (type1, type2) -> {
if (type1.equals(type2)) {
return 0;
}
if (type2.equals(ParticipantType.S.getDescription())) {
return 1;
}
if (type1.equals(ParticipantType.S.getDescription())) {
return -1;
}
return type1.compareTo(type2);
}))
.collect(toList());
}
This is my enum:
@Getter
public enum ParticipantType {
F("F"),
D_F("D+F"),
S("S");
private final String description;
ParticipantType(String description) {
this.description = description;
}
}