0

I have a list object type and I want to sort it by date in ascending order. First of all I get the resevations that are between these dates and saving it to a new List. Now I need someway to sort it. I tried Collections.sort(reservationsByDate) & Collections.sort(reservationsByDate, Collections.reverseSort() , but it didn't produce anything. I'm kinda new to java so if theres something that im missing please help me implement this.

heres my code:

public List<Reservation> getAllReservationsSortedByDate(LocalDate from, LocalDate to) {
    int fromDate = from.getDayOfMonth();
    int toDate = to.getDayOfMonth();
    ArrayList<Reservation> reservationsByDate = new ArrayList<>();
    for (Reservation reservation : reservations) {
        if (reservation.getFromDate().getDayOfMonth() >= fromDate && reservation.getToDate().getDayOfMonth() <= toDate) {
            reservationsByDate.add(reservation);
        }
    }
    //reservationsByDate needs to be sorted by dates in ascending order...
    return reservationsByDate;
}

Thank you for your help.

  • 1
    Does this answer your question? [Sort objects in ArrayList by date?](https://stackoverflow.com/questions/5927109/sort-objects-in-arraylist-by-date) – Matt Apr 08 '22 at 20:23
  • *it didn't produce anything* Did you mean it produced an empty list, an unsorted list, no list at all or what? – Ole V.V. Apr 09 '22 at 08:45

1 Answers1

0

You are iterating over "reservations". The definition of this field is not shown. If it is empty the result would always be an empty list.

GJohannes
  • 1,408
  • 1
  • 8
  • 14