Change default global sort order for collections (ascending or descending) (👍11) #4013
Replies: 4 comments
This comment has been hidden.
This comment has been hidden.
-
|
Providing additional details here as a use case. Typically need to have collections sorted in date descending orderThe option to reverse pagination arrays in #194 has been a boon, allowing me to simplify the curation of collections using tags in data files, rather than globbing files in the config file. However, in pretty much every instance, I am having to use Difficulty reversing collections correctly in Liquid templatesI have added a {% for item in collections.post limit: 25 reversed %}
…
{% endfor %}Without limiting the number of items returned, items are returned as expected (all items tagged To remedy, I have created the following collection within my config file: eleventy.addCollection('post', collection => {
return collection.getFilteredByGlob('**/+(articles|bookmarks|notes|photos|presentations|projects)/**/*.md').reverse();
});That is to say, reversing the array of collections earlier in the build process makes manipulating them much easier within templates. I hope this information proves useful when considering the design of this feature. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @paulrobertlloyd - came across this while having my own This had no effect (for me even without a limit). Posts were still displaying in the default ascending order. I went ahead and opted to reverse them in the config as you have done instead. Seems that the liquid filter should work tho, no? |
Beta Was this translation helpful? Give feedback.
-
|
As an input from my Jekyll to Eleventy migration, it would be good if Eleventy behaved as jekyll out-of-the-box to bring them closer and make adoption and migration easier. As @paulrobertlloyd, I needed to used My work around was to break down the filters in two steps with an assign: {% assign posts = collections.posts | reverse %}
<ul>
{%- for post in posts limit: 7 -%}
<li>{{ post.data.title }}</li>
{%- endfor -%}
</ul>While in Jekyll this is only: {% for post in site.posts limit: 7 %}
<li>{{ post.data.title }}</li>
{% endfor %}If we could bring these two closer, via Making collections reversed by default, from recent to oldest as in Jekyll might be a good start, though a breaking change. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Surfaced in #192 by @paulrobertlloyd #194 (comment)
By default everything is ascending, expose a global configuration option to sort everything descending.
Beta Was this translation helpful? Give feedback.
All reactions