Sort pages by modiﬁed data / custom metadata

Hi! Is there a way to sort entries by modiﬁed date, or by some custom numeric metadata ﬁeld in posts?

For example, if I have something like:

        {{#allEntries}}
{{#tagged.Home}}
<li><a href="{{url}}">{{title}}</a></li>
{{/tagged.Home}}
{{/allEntries}}

Can I specify the order in which these entries appear? Even the ability to sort by modiﬁed date might be enough, since I can touch” each ﬁle to put them in the correct order.

Thank you!

1 replies Asked Jun 16 '21 at 17:49 · Improve this question

Not right now – but I would like to add this in future. Would you like to be notiﬁed when native sorting is added to the template engine? If so, please let me know via email. I’m still deciding on the syntax but I eventually envision something like this perhaps:

{{#posts sort:updated tagged:home}}
<li><a href="{{url}}">{{title}}</a></li>
{{/posts sort:updated tagged:home}}

Anyway, for now please do this on the front-end with JavaScript. There are a number of neat solutions to sort nodes by a particular attribute in this Stack Exchange question: Easiest way to sort DOM nodes?.

Here’s a complete example for Blot which sorts posts by their updated date:

<ul id="home-list">
{{#allEntries}}
{{#tagged.Home}}
<li data-updated="{{updated}}">
<a href="{{url}}">{{title}}</a>
</li>
{{/tagged.Home}}
{{/allEntries}}
</ul>

<script type="text/javascript">
var list = document.querySelector('#home-list');
[...list.children]
.sort((a,b)=>a.getAttribute('data-updated')>b.getAttribute('data-updated')?1:-1)
.forEach(node=>list.appendChild(node));
</script>