Wrong image sort by name

Hello,
i have a gallery like:
`

                    <?php foreach($page->images()->sortBy('name', 'desc') as $pic): ?>
                    <li class="colA">
                        <a data-strip-options="maxWidth: 800" target="_blank" data-strip-caption="<?php echo html($pic->caption()); ?><?php if(!$pic->Link()):?> — <a href='http://<?php echo html($pic->Link()) ?>'><?php echo html($pic->Link()) ?></a><?php endif;?>" data-strip-group="shared-options" class="strip" href="<?php echo $pic->url(); ?>">
                            <?php echo thumb($pic, array('width' => 300,'height' => 410));?>
                        </a>
                    </li>
                    <?php endforeach ?>`
    

    and a file structure like:
    1-file.jpg
    2-file.jpg
    3-file.jpg
    …
    10-file.jpg
    11-file.jpg

    But in kirby it is sortet wrongly like:
    1-file.jpg
    10-file.jpg
    11-file.jpg
    2-file.jpg
    3-file.jpg

    What can i do?

You can use a sort flag as the third parameter

<?php foreach($page->images()->sortBy('name', 'desc', SORT_NUMERIC) as $pic): ?>
  ...           

https://getkirby.com/docs/cheatsheet/files/sortBy
http://php.net/manual/de/function.sort.php

1 Like

Hi, i have the same issue. I’d like to display my image files like:

1
2
3
4
5
…
11

<?php foreach($page->images()->sortBy('name', 'desc', SORT_NATURAL) as $image): ?>

gives me the files in the wrong order

11
…
5
4
3
2
1

<?php foreach($page->images()->sortBy('name', 'desc', SORT_NUMERIC) as $image): ?>

does not solve the problem as well, the files are displayed as

1
11
…
2
3
4
5

Could isotope be the problem? I have no clue how to solve it. Thx for help in advance :slight_smile:

Looks like SORT_NATURAL returns the right order, though? But you should make it asc instead of desc. If you think Isotope could be the culprit, I’d disable it for the moment and check if the result is correct.

<?php foreach($page->images()->sortBy('name', 'asc', SORT_NUMERIC) as $image): ?>

Makes it! Thank you

1 Like