Sort tags alphabetically …

Dear community,

once more I implemented filtering with tags in one of my projects.

But unfortunately I’m struggling to have the tags in my for-loop sorted, e.g. alphabetically …

controller:

<?php
return function($page) {

  // fetch the basic set of pages
  $files = $page->files();

  // fetch all tags
  $tags = $files->pluck('tags', ',', true);

  // add the tag filter
  if ($tag = param('tag')) {
    $files = $files->filterBy('tags', urldecode($tag), ',');
  }

  return compact('files', 'tags', 'tag');
};

snippet:

<ul id="tags">
  <li class="tag<?php e(kirby()->request()->params()->tag() == null, ' active') ?>"><a href="<?= $page->url() ?>">Show All</a></li>
<?php foreach($tags as $tag): ?>
  <li class="tag<?php e(kirby()->request()->params()->tag() == urlencode($tag), ' active') ?>"><a href="<?= url($page->url(), ['params' => ['tag' => urlencode($tag)]]) ?>"><?= html($tag) ?></a></li>
<?php endforeach ?>
</ul>
$tags = $files->pluck('tags', ',', true);
sort($tags);
3 Likes

Wow, that was way easier then expected. I tried lots of things with sortBy and tried to sort the $tags array … phew.

Thank you so very much, as always, @texnixe.