-
-
Save Phlow/1f27dfafdf2bbcc5c48e to your computer and use it in GitHub Desktop.
{% 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> |
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 %}
...
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
It works now without parenthesis.
<ul>
{% assign sorted = site.projekte | sort: 'date' | reverse %}
{% for item in sorted %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
ty!
👍
Thanks so much for this, seriously 👍
@iamalfonse Your‘re welcome. This is how I feel, if I find useful code snippets :)
Thanks, saved me screwing it up in a hurry. Elegant solution.
@NixImagery You're welcome. That's what gists are for :)
Great, thank you so much!!
@NavidFehren You‘re welcome.
Thank you so much!!!
@minheeyoon You‘re welcome. 😸
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.
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?
Thanks so much for sharing this!
According to Liquid Documentation, now you can just do:
I tried it on my theme and both methods give the same output. 😉