I'm having trouble figuring out how to sort the content of a collection by category. I've found a very similar question on how to sort posts by category but it doesn't seem to work with collections Jekyll display posts by category.
Let's say I have a collection called 'cars' with two categories: 'old' and 'new'. How can I display only cars with the category 'old'?
You can sort, group or filter collection like any other object like page or posts.
_my_collection/mustang.md
---
category: 'old'
abbrev: tang
---
mustang content
_my_collection/corvette.md
---
category: 'new'
abbrev: vet
---
corvette content
{% assign newcars = site.my_collection | where: "category", "new" %}
{% assign allcarssorted = site.my_collection | sort: "category" %}
{% assign groups = site.my_collection | group_by: "category" | sort: "name" %}
This groups your cars and then sort groups by name that is the category.
{% for group in groups %}
{{ group.name }}
{% for item in group.items %}
{{item.abbrev}}
{%endfor%}
{%endfor%}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With