Sort pages by modified data / custom metadata
Hi! Is there a way to sort entries by modified date, or by some custom numeric metadata field 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 modified date might be enough, since I can "touch" each file to put them in the correct order.
Thank you!
Not right now – but I would like to add this in future. Would you like to be notified 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>
Answered 3 years ago ·
Edit answer