Skip to content

Instantly share code, notes, and snippets.

@Phlow
Last active April 30, 2024 13:30
Show Gist options
  • Select an option

  • Save Phlow/1f27dfafdf2bbcc5c48e to your computer and use it in GitHub Desktop.

Select an option

Save Phlow/1f27dfafdf2bbcc5c48e to your computer and use it in GitHub Desktop.
This Liquid loop for Jekyll sorts a collection by date in reverse order
{% comment %}
*
* This loop loops through a collection called `collection_name`
* and sorts it by the front matter variable `date` and than filters
* the collection with `reverse` in reverse order
*
* To make it work you first have to assign the data to a new string
* called `sorted`.
*
{% endcomment %}
<ul>
{% assign sorted = site.collection_name | sort: 'date' | reverse %}
{% for item in sorted %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
@nelsonauner

Copy link
Copy Markdown

👍

@claudiomateluna

Copy link
Copy Markdown

life saving thanks a lot

@stegua

stegua commented Jan 2, 2017

Copy link
Copy Markdown

Thanks, tip quite useful... and it was the first result I found on Google!

@asilvadesigns

Copy link
Copy Markdown

Awesome, thanks!

@uboubo

uboubo commented Feb 27, 2017

Copy link
Copy Markdown

Thanks a lot for your solution!
In my case, I had to change
{% for item in sorted %}
to
{% for collection_name_item in sorted %}.
But again - thank you very much!

@johnpitchko

Copy link
Copy Markdown

Just what I needed, thanks! More reason to learn Liquid.

@sio4

sio4 commented Sep 28, 2017

Copy link
Copy Markdown

Great! I cannot found it from official site and github repository of jekyll! Thanks!
What I changed is:

{% assign projects = (site.projects | sort: 'date') | reverse %}

to

{% assign mid = site.projects | sort: 'date' %}
{% assign projects = mid | reverse %}

since I faced with the following message:

Liquid Warning: Liquid syntax error (line 1): Expected dotdot but found pipe in "{{(site.projects | sort: 'date') | reverse }}" in projects.html

@sio4

sio4 commented Sep 28, 2017

Copy link
Copy Markdown

I tested again with some others, and code below also works for my env:

{% assign projects = site.projects | reverse %}
{% for post in projects %}
<...>

@Jrschellenberg

Copy link
Copy Markdown

A wonderful, concise solution. You have my thanks!

@anshulsamar

Copy link
Copy Markdown

Thank you!!

@88kbbq

88kbbq commented May 1, 2018

Copy link
Copy Markdown

I'm going nuts here trying to loop through all items in a collection.
The index.md uses the home.html template
I want to sort all the items in my tabs collection to display in my home template:

<div id="content">
 	{% for item in site.tabs %}
        {% assign item = site.tabs | sort: 'order' %}
 	{{ item }}
 	{% endfor %}
</div>

The items in the tabs collection use the tab.html template:

<div class="wrapper red">
<div class="tag">
    <div class="tag-title jcenter acenter center">
      <h1 class="title center">{{ item.title }}</h1>
    </div>
    <div class="tag-text center">{{ content }}</div>
    <div class="tag-action center">
      <a class="h3 barbecue-btn my2" href="{{ site.baseurl }}/{{ item.link }}">{{ item.action }}</a>
    </div>
</div>
</div>

An item in the tabs collection has yaml:

---
layout: "tab"
title: "AUTHENTIC AMERICAN BARBECUE"
link: "locations.html"
action: "Find Locations"
order: "1"
---

Treat your family, friends, and special guests to real American BBQ at your next event.

Help

@kyletaylored

Copy link
Copy Markdown

I'm going nuts here trying to loop through all items in a collection.
The index.md uses the home.html template
I want to sort all the items in my tabs collection to display in my home template:

@88kbbq you actually seem to be doing it right, just remove the quotes from your order property in your front matter. I used the same approach and it's working for me. (I know this was months ago, but figured for the future lurkers!)

@rmfranciacastillo

Copy link
Copy Markdown

Sweet script! I was just looking for something like this 👍

@sylhare

sylhare commented Apr 29, 2019

Copy link
Copy Markdown

According to Liquid Documentation, now you can just do:

{% for item in site.collection_name reversed %}

I tried it on my theme and both methods give the same output. 😉

@ccfz

ccfz commented Sep 25, 2019

Copy link
Copy Markdown

I needed the collection to be sorted based on a specific date. Here is my code:

{% assign sortedWorkshops = site.workshops | sort: 'start_date' | reverse %}

      {% for workshop in sortedWorkshops %}

...

@timothy-kodes

Copy link
Copy Markdown

Great! I cannot found it from official site and github repository of jekyll! Thanks!
What I changed is:

{% assign projects = (site.projects | sort: 'date') | reverse %}

to

{% assign mid = site.projects | sort: 'date' %}
{% assign projects = mid | reverse %}

since I faced with the following message:

Liquid Warning: Liquid syntax error (line 1): Expected dotdot but found pipe in "{{(site.projects | sort: 'date') | reverse }}" in projects.html

It gives this error because you cannot use parenthesis

@Phlow

Phlow commented Jul 27, 2020

Copy link
Copy Markdown
Author

It works now without parenthesis.

<ul>
    {% assign sorted = site.projekte | sort: 'date' | reverse %}
    {% for item in sorted %}
    <li>{{ item.title }}</li>
    {% endfor %}
</ul>

@kuz3

kuz3 commented Aug 3, 2020

Copy link
Copy Markdown

ty!

@puppylpg

puppylpg commented Sep 4, 2020

Copy link
Copy Markdown

👍

@iamalfonse

Copy link
Copy Markdown

Thanks so much for this, seriously 👍

@Phlow

Phlow commented Oct 21, 2020

Copy link
Copy Markdown
Author

@iamalfonse Your‘re welcome. This is how I feel, if I find useful code snippets :)

@NixImagery

Copy link
Copy Markdown

Thanks, saved me screwing it up in a hurry. Elegant solution.

@Phlow

Phlow commented Dec 19, 2020

Copy link
Copy Markdown
Author

@NixImagery You're welcome. That's what gists are for :)

@NavidFehren

Copy link
Copy Markdown

Great, thank you so much!!

@Phlow

Phlow commented Jan 23, 2021

Copy link
Copy Markdown
Author

@NavidFehren You‘re welcome.

@ds-package

Copy link
Copy Markdown

Thank you so much!!!

@Phlow

Phlow commented May 11, 2021

Copy link
Copy Markdown
Author

@minheeyoon You‘re welcome. 😸

@wdzajicek

Copy link
Copy Markdown

The date key is now default

As of Jekyll > v4.0.0 date, is the default key used for sorting collection items (provided the collection items have date keys.) The original liquid variable-assignment can be shortened:

<!-- `| sort: 'date '` can be omitted -->
{% assign sorted = site.collection_name | sort: 'date' | reverse %}
<!-- This will accomplish the same thing  -->
{% assign sorted = site.collection_name | reverse %}

By default, two documents in a collection are sorted by their date attribute when both of them have the date
key in their front matter. However, if either or both documents do not have the date key in their front matter,
they are sorted by their respective paths.
https://jekyllrb.com/docs/collections/#custom-sorting-of-documents

See Custom Sorting of Documents on the Collections page of Jekyll docs for more info.

@Phlow

Phlow commented Sep 3, 2021

Copy link
Copy Markdown
Author

I haven't tested your suggestion. But you write provided the collection items have date keys.

I think for being save it doesn't hurt, in case no date is associated, it doesnt count. What do you think?

@prabhav

prabhav commented Oct 11, 2021

Copy link
Copy Markdown

Thanks so much for sharing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment