Issue sorting pages with multiple dates

I’m working on a website for a film festival.
Each movie has multiple screening dates.

In the program page I want to display all the movies and sort them by date.
Each movie appears multiple times according to its dates.

For example:

DAY 1

  • Movie A

DAY 2

  • Movie B
  • Movie C

DAY 3

  • Movie A
  • Movie B

All works well, but I have a issue in displaying the dates in the correct order.
For some reason one day gets mixed up and I have:

  • DAY 2
  • DAY 1
  • DAY 3

In the movie blueprint the dates are collected like this:
blueprints/movie.yml

title: Movie

tabs:
  content:
    columns:
      - width: 2/3
        fields:
          playDates:
            type: structure
            label: Play Dates
            translate: false
            min: 1
            fields:
              playDate:
                type: date
                time: true
                required: true
                label: Date + Time
                display: DD. MMMM YYYY,

I use a controller to group the movies into a collection for the program page:
controllers/program.php

<?php

return function ($kirby, $page) {
    // Create a new collection for movies with dates
    $moviesWithDates = [];

    // Loop through each movie and add it to the collection along with the date
    foreach ($page->children()->last()->children()->listed() as $movie) {
        $dates = $movie->playDates()->toStructure()->sortBy('playDate', 'asc');
        foreach ($dates as $date) {
            $formattedDate = $date->playDate()->toDate('l d. M');
            $moviesWithDates[] = [
                'movie' => $movie,
                'date' => $formattedDate,
            ];
        }
    }

    // Group the movies by date
    $moviesWithDatesCollection = new Collection($moviesWithDates);
    $groupedMovies = $moviesWithDatesCollection->groupBy('date');

    return compact('groupedMovies');
};

In the program page I then load a snippet to display the right movie entry under each day:
snippets/program-entry.php

<section>
  <?php foreach ($groupedMovies as $date => $movies): ?>
    
    <!-- Single Days -->
    <div>

      <!-- Date header -->
      <h2>
        <?= $date ?>
      </h2>
      
      <!-- Movie Entries -->
      <div>
        <?php foreach ($movies as $movieWithDate): ?>

          <?php if($movieWithDate['movie']->screeningType() == 'Single Movie'): ?>
            <?php snippet('pages/program/single-movie', ['movieWithDate' => $movieWithDate, 'date' => $date]) ?>
          <?php endif ?>

          <?php if($movieWithDate['movie']->screeningType() == 'Shorts'): ?>
            <?php snippet('pages/program/shorts', ['movieWithDate' => $movieWithDate, 'date' => $date]) ?>
          <?php endif ?>

          <?php if($movieWithDate['movie']->screeningType() == 'Workshop'): ?>
            <?php snippet('pages/program/workshop', ['movieWithDate' => $movieWithDate, 'date' => $date]) ?>
          <?php endif ?>

        <?php endforeach ?>
      </div>
      
    </div>
    <?php endforeach ?>
  </section>

I tried to play around with sortBy but with no luck.
One other problem I’m facing is that in the movie blueprint I cannot sort the entry by playDate
I tried sortBy: playDate asc but it doesn’t work.

Might be there the issue?

Any input is very much appreciated!

Alright, I solved it.

The problem was the format of the date.
I now used ‘Y-m-d’ for sorting and then converted to ‘l d. M’ for display.

For reference:

<?php

return function ($kirby, $page) {
    // Create a new collection for movies with dates
    $moviesWithDates = new Collection();

    // Loop through each movie and add it to the collection along with the date
    foreach ($page->children()->last()->children()->listed() as $movie) {
        $dates = $movie->playDates()->toStructure()->sortBy('playDate', 'asc');
        foreach ($dates as $date) {
            $formattedDate = $date->playDate()->toDate('Y-m-d'); // sorting should be done on this format
            $moviesWithDates->add([
                'movie' => $movie,
                'date' => $formattedDate,
            ]);
        }
    }

    // Group the movies by date
    $groupedMovies = $moviesWithDates->sortBy('date', 'asc')->groupBy('date');

    // Convert back the date format to 'l d. M' for display
    foreach ($groupedMovies as $date => $movies) {
        $dateFormattedForDisplay = date('l d. M', strtotime($date));
        $groupedMoviesForDisplay[$dateFormattedForDisplay] = $movies;
    }

    return compact('groupedMoviesForDisplay');
};