Sort only if sort exist

Hi,

I write this :

$page->images()->filterBy('template', 'image')->sortBy('sort');

But if there is no reorder (no drag), sort is empty and order is not as expected (based on filename).

I miss something ?

<?php foreach ( $page->images()->filterBy('template', 'image')->sortBy(function($file) {
  if ($file->sort()->isNotEmpty()) {
    return $file->sort();
  }
  return $file->filename();
}) as $file) {
  echo $file->sort() . '-' . $file->name();
}

Will sort them by sort first, then by filename

1 Like

This topic contains a short solution:

1 Like

$page->images()->filterBy('template', 'image')->sortBy('sort','asc', 'filename', 'asc');

Kirbyly Perfect !