Sort by date + the time the event starts

Hello!
I try to use sorting for the events - right now I sort it by date.
I would want it to sort by the time the event starts as well

this is my code at the moment:

<?php   
                $today = date('Y-m-d');
                $events = page()->children()->template('event')
                ->filter(fn ($p) => $p->date()->toDate('Y-m-d') === $today);

                $sortedEvents = $events->sortBy(function ($page) {
                return $page->date()->toDate();
                });

                foreach($sortedEvents->limit(2) as $event): ?>
                    <?php snippet('event', ['event'=>$event])?>
                
        <?php endforeach ?>

any idea how can I achieve that?

this is my yml:

    sections:
      times:
        type: fields
        fields:
          date:
            label: date
            type: date
            display: DD MMMM YYYY
          starts:
            label: time starts
            type: time
            display: HH:mm
          ends:
            label: time ends
            type: time
            display: HH:mm

I would appreciate any help - thanks !

Try this

$sortedEvents = $events->sortBy('date','desc','starts','asc');
1 Like

It works perfectly
Thanks a lot!