$pages->sortBy()
Alias for Kirby\Cms\Pages::sort()
$pages->sortBy(string|callable $field, string|null $direction, int|null $method): Kirby\Cms\Pages
Parameters
Name | Type | Default | Description |
---|---|---|---|
$fieldrequired | string orcallable |
no default value | Field name or value callback to sort by |
$direction | string ornull |
no default value | asc or desc |
$method | int ornull |
no default value | The sort flag, SORT_REGULAR, SORT_NUMERIC etc. |
Return types
This method does not modify the existing $pages
object but returns a new object with the changes applied. Learn more →
Parent class
Kirby\Cms\Pages
inherited from Kirby\Toolkit\Collection
Examples
<ul>
<?php foreach($page->children()->sortBy('title', 'desc') as $subpage): ?>
<li>
<a href="<?= $subpage->url() ?>">
<?= $subpage->title()->html() ?>
</a>
</li>
<?php endforeach ?>
</ul>
It's also possible to sort by more than one field:
$articles = page('notes')->children()->sortBy('date', 'desc', 'time', 'asc');
For more complex sorting examples, check out our "Sorting" recipe