Sort list field alphabetically?

I’m using the Kirby List Field and was wondering whether there is a way to sort the output alphabetically? Its seems to me that sortBy()would not work in this context, is there an alternative way of doing this?

What are you storing in your list field? And what exactly do you want to sort? SortBy() works with any collection and you can turn any array into a collection, so that would indeed work.

Apologies, I want to sort alphabetically by name of the company. In the txt file it would look like

Clients: 

- Nike
- Adidas
- Reebok
- Under Armour

How can I turn it into a collection?

Easiest is just to sort the array:

$array = $page->clients()->yaml();
asort($array);
dump($array);

With a new Collection:

$collection = new Collection($page->clients()->yaml());
dump($collection->sort());

Thank you. How would I use I this in a foreach loop? Currently, I get the error:

asort() expects parameter 1 to be array, object given

<?php $array = $page->clients() ?>
<?php foreach(asort($array) as $client): ?>
    <div><?= $client->value() ?></div>
<?php endforeach; ?>

Try…

<?php 
$clients = $page->clients()->yaml();
asort($clients);
?>
<?php foreach($clients as $client): ?>
    <div><?= $client ?></div>
<?php endforeach; ?>

or you can use ->toStructure() instead and and sort on that, which is slicker in my opnion. See Here.