Custom Sort for multiple attributes In java

76 views Asked by At

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.

  1. Sort first based on BoardPoint
  2. If BoardPoint is the same then we need to sort based on OffPoint.
  3. If BoardPoint and OffPoint are also same then we need to sort based on LastName
  4. 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.

2

There are 2 answers

1
ashish gupta On
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())
                    .thenComparingInt(row -> row.getFlights().size()) // Compare based on the number of flights
                    .thenComparingInt(row -> row.getNames().size())   // Compare based on the number of names
                    .thenComparing(row -> {
                        // Compare each flight within the row
                        List<ReservationFlight> flights = row.getFlights();
                        return Comparator.comparing(ReservationFlight::getBoardPoint)
                                .thenComparing(ReservationFlight::getOffPoint)
                                .compare(flights.get(0), flights.get(flights.size() - 1));
                    })
                    .thenComparing(row -> {
                        // Compare each name within the row
                        List<ReservationName> names = row.getNames();
                        return Comparator.comparing(ReservationName::getLastName)
                                .thenComparing(ReservationName::getFirstName)
                                .compare(names.get(0), names.get(names.size() - 1));
                    }))
            .toList();
}
0
Souhaila On

You should explain more and provide us with additional data example.

If you have more than one ReservationName/ReservationFlight and you want to sort them you should sort them before doing the global sort.

In addition, comparing the first element in the list is upon to your desicion, you may choose the first, last, middle or random element.

I can imagine your example as comparing school classes with 2 lists of students grades and students names. So in this case, for example you can compare the average grades of students or the minimum grade or maximum. It is simply upon to you how do you sort every list in the object.