Description

This code-snippet is inspired by this one, the user Christian Specht posted on stackoverflow.

The problem

I want my list of tags to be sorted alphabetically and all the upper-case tags integrated in the list.

List with the mentioned solution from Christian Specht:

  • ABCD
  • BCDE
  • abcd
  • bcde

List with my improved code:

  • ABCD
  • abcd
  • BCDE
  • bcde

A solution

<h1>Tags</h1>
<ul>
  {% capture tags %}
    {% for tag in site.tags %}
      {{ tag[0] | downcase }},{{ tag[0] }}{% unless forloop.last %}|{% endunless %}
    {% endfor %}
  {% endcapture %}
  {% assign sortedtags = tags | split:'|' | sort %}

  {% for tag in sortedtags %}
    {% assign taglist = tag | split:',' %}
      <li><a href="/tag/{{ taglist[1] }}">{{ taglist[1] }}</a></li>
  {% endfor %}
</ul>

Notes

Because of the way how arrays work in liquid, with this solution you are not able to include pipe ('|') or commas (',') inside of tags. But you are free to choose another delimiter and split on that one.