Kevin Falting
Kevin Falting

Reputation: 527

Jekyll: How to change the default ordering of collections

I have a collection in my jekyll site that has files named as numbers. 1, 2, 3, ... 10, 11, 12, and so on. I'm building these pages to connect with each other, so 1 would connect to 2, ...

When I build, jekyll serves it in the order of 1, 10, 11, 12, 2, 3, ...

How can I have it build in proper numerical order?

Upvotes: 6

Views: 1991

Answers (2)

Mr. Hugo
Mr. Hugo

Reputation: 12592

First, add order_number to your YML, like this:

---
title: anything
order_number: 10
---

Then, use the following code to order your pages:

{% assign ordered_pages = site.pages | sort:"order_number" %}
{% for page in ordered_pages %}
  <a href="{{ page.url | relative_url }}">{{ page.title }}</a>
{% endfor %}

I use '_number' to be compatible with CloudCannon (specifying the input type). If you don't care about that, you can just use 'order' as a variable name. I also number like this: 10, 20, 30, 40, etc. This enables me to insert new pages without changing everything.

Upvotes: 2

Kevin Falting
Kevin Falting

Reputation: 527

After a little more searching, I found this answered question: How to change the default order pages in jekyll

Basically, what I came up with was:

{% assign ordered_pages = site.pages | sort:"title" %}
{% for page in ordered_pages %}

<a href="{{ page.url | relative_url }}">{{ page.title }}</a>

{% endfor %}

Which is nearly identical to the original answer.

Upvotes: 6

Related Questions