如何用液体按字母顺序对类别进行排序?

How to sort categories alphabetically with liquid?

我试图在 Jekyll 中按字母顺序而不是按时间顺序对 post 类别进行排序。我每次尝试这样做都会导致字符串到整数错误。有什么建议吗?

<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>

你可以使用 jekyll sort 过滤器

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