Sorting a tag array and ouputting the first letter for each tag

my question: I want to retrieve all tags used on the page (guess I could do that with $tags = $site->children()->pluck('tags', ',', true);) and then looping over them, sort them alphabetically ($sorted = a::sort($tags, 'title', 'asc');) in sections each starting with their first letter (how?), eg:

Aaa tag
Abb tag
Acc tag

Baa tag
Bbb tag
Bcc tag

// and so on ..

I also found this plugin but do I really need two plugins to be installed … isn’t there another way as of Kirby 2.5.6?

Thank you very much, any pointing in the right direction helps!

I’d suggest to create an collection from your array first and then use group()on it:

$tags = $site->index()->pluck('tags', ',', true);
$collection = new Collection($tags);
$groupedItems = $collection->group(function($item){
  return substr($item, 0, 1);

Then you can loop through the collection like here in the docs:


Since I always see this: when can you omit the Kirby function first letter?

sort($tags) = a::sort($tags) ??

sort() is a standard PHP function; a::sort() is a method of the toolkit’s A class. Two different things.

I can totally use this, too - nice one.

Maybe that might be interesting for kirby secrets - what’s your opinion on this, @jenstornell?

1 Like

I’m planning to integrate a section with short code snippets/tips and tricks in the docs in the future. So something like this might find its way into it.

1 Like