Search code examples
loopsjekyllliquid

How to sort categories alphabetically with liquid?


I am trying to sort post categories alphabetically rather than chronologically in Jekyll. Each attempt I make to do so results in string to integer errors. Any suggestions?

<div>
{% for category in site.categories %}
  <div class="archive-group">
    {% capture category_name %}{{ category | first }}{% endcapture %}

    <div id="#{{ category_name | slugize }}"></div>

    <h3 class="category-head"><small>{{ category_name }}</small></h3>
    <a name="{{ category_name | slugize }}"></a>

    {% for post in site.categories[category_name] %}
    <article class="archive-item">
      <a href="{{ post.url }}">{{ post.title }}{% if post.link %}<span class="link-arrow"> &rarr;</span>{% endif %}</a>
    </article>
    {% endfor %}
  </div>
{% endfor %}
</div>

Solution

  • You can use jekyll sort filter

    {% assign categories = site.categories | sort %}
    {% for category in categories %}
    ...