Alphabetize Tags

So I’ve created a working tag cloud that displays all restaurant category tags (with one fake tag “All” persistent to the front).

    <?php $tags = $page->children()->pluck('tags', ',', true); ?>
    <ul class="tags-list">
        <a href="<?php echo $page->url() ?>"><li class="label">All</li></a>
            <?php foreach($tags as $tag): ?>
    	        <a href="<?php echo $page->url() . '/tag:' . $tag ?>">
                    <li class="label"><?php echo html($tag) ?></li>
                 </a>
    	    <?php endforeach ?>
    	</ul>

It works great. However, I’ve gotten a request to alphabetize the results. I cannot for the life of me figure it out. If I add

->sortBy('tags', 'asc')

before pluck(), then it looks like it alphabetizes each result, then groups them. What we would like is for the entire tag cloud to be alphabetized after gathering the results. Any ideas?

And thank you!

If you sort before pluck()ing, you effectively only sort by the first tag of each page, which will lead to a strange but incorrect sorting. So you need to sort the result of the pluck operation using the standard PHP sort function:

<?php $tags = sort($page->children()->pluck('tags', ',', true)); ?>

That sounds logical to me, however, implementing it is causing this:

Strict Standards: Only variables should be passed by reference

Do I need to have the tags presented as an array first?

Ah, sorry about that. You need to save it into a local variable first:

<?php $tags = $page->children()->pluck('tags', ',', true); ?>
<?php sort($tags); ?>
2 Likes

Derp. I figured it out. I just passed $tags into sort() afterwards:

<?php $tags = $page->children()->pluck('tags', ',', true);
      sort($tags); ?>

Thanks for your help!!