Sorting by date in the opposite order

Hi Everyone,
I try to sort events by date, but in the opposite order. for example, right now it shows the events from the earliest to the latest:

  1. may 1st
  2. june 20th
  3. july 19th

I want it to appear in a reverse order:

  1. july 19th
  2. june 20th
  3. may 1st

this is my code:

 <?php
            $events = page()->children()->listed();
            $sortedEvents = $events->sortBy(function ($page) {
            return $page->starts()->toDate();
            });

            foreach($sortedEvents as $event): ?>
                <div class='event'>
                <div class='event-dates'>
                    <p><?= $event->starts()->toDate('d F') ?></p>
                </div>
            </div>
<?php endforeach ?>

I noticed that there is the flip() method but Im not sure where to use it.
Thanks in advance!

You can either pass a second attribute desc to the sortBy function or flip after sorting:

   $sortedEvents = $events->sortBy(function ($page) {
            return $page->starts()->toDate();
            }, 'desc');
   $sortedEvents = $events->sortBy(function ($page) {
            return $page->starts()->toDate();
            })->flip();

Great- Thank a lot! it works!