I have the below method for custom code to Sort and the requirement is that we have to do the custom sort based on BoardPoint, OffPoint, LastName, and FirstName.
- Sort first based on BoardPoint
- If BoardPoint is the same then we need to sort based on OffPoint.
- If BoardPoint and OffPoint are also same then we need to sort based on LastName
- If BoardPoint, OffPoint, and LastName are the same then we have to sort based on firstname.
My code:
private List<ReservationRow> sortRows(List<ReservationRow> reservationRows) {
return reservationRows.stream()
.sorted(Comparator.comparing(row -> row.getFlights().get(0).getBoardPoint())
.thenComparing(row -> row.getFlights().get(0).getOffPoint())
.thenComparing(row -> row.getNames().get(0).getLastName())
.thenComparing(row -> row.getNames().get(0).getFirstName()))
.toList();
}
class ReservationRow {
private List<ReservationName> names;
private List<ReservationFlight> flights;
//setters and getters
}
class ReservationName {
private String lastName;
private String firstName;
}
class ReservationFlight {
private String boardPoint;
private String offPoint;
}
Here I am doing sorting only with row.get(0).
What if we have multiple names and flights, because they are List. So I have to sort internal also.
I have done some of my research but could not understand this better.