Manual sorting on files section does not reproduce on frontend until at least one file dragged

I have some pages with a files section with default manual sorting, this is the yml:

			headline: Images
			type: files
			template: image
			layout: cards
			size: small
			info: "{{}} - {{file.horizontalWidth.fractionToEmoji}}"
			min: 1

…I want to show these images on the frontend in the order they appear in the panel, so I am using: <?php foreach($page->images()->sortBy('sort', 'asc') as $work): ?>

The problem is that if I add several images to the section, the order they appear in the section is not the order they appear in the frontend UNLESS at least one of the files has been manually dragged in the panel.

Once at least one image is dragged to another position in the files section, then the sorting order is properly reproduced in the frontend, using the foreach code above.

Am I forgetting something, doing something wrong or is this some sort of bug

Thank you

Am I perhaps misunderstanding this note on the guide?:

Note that the default sorting order of files is according to their order in the file system. To sort files by their manual sorting order, you can use $files->sortBy('sort') , where sort is the field that stores the sorting number if you manually sort files in the Panel.

I remember there is an issue on GitHub. But if you upload new files, they don’t get a sorting number unless they are sorted by drag&drop- So sorting by the sort field will only work as expected if the files all have a sorting number.

Oh I see Hmm… nobody found this annoying before? perhaps a workaround? I will search github Thanks

Hm, manual sorting means manual sorting. What should happen with those files if they are not manually sorted?


In that issue it suggests

$files->sortBy('sort', 'asc', 'filename', 'asc');

…does this code imply that if there is no sort number, files are sorted by filename ? And does that suggest that if I drop some files in the files section, the order they take is by filename ? Well actually that was easy to test and it seems like it.

So if the above code does what I understood is a perfect solution, thank you, I am going to test

Yes, exactly, this is how it is done in kirby\config\section\files.php:

if ($this->sortBy) {
    $files = $files->sortBy(...$files::sortArgs($this->sortBy));
} elseif ($this->sortable === true) {
    $files = $files->sortBy('sort', 'asc', 'filename', 'asc');

Yep, that works like a charm, perfect, thank you