I found a better solution for this. Looking at the Kirby source (kirby/config/api/routes/files.php
), you can pass multiple parameters to sortBy
. It will fallback to filename if sort order does not exist.
foreach($page->images()->sortBy('sort', 'asc', 'filename', 'asc') as $image):