Home > database >  How do I sort an array of post objects?
How do I sort an array of post objects?

Time:11-11

I'm working on a Jekyll site and want to sort the post list on the homepage by post.date-edit rather than by post.date. However, it seems like the sort filter only works on strings. How do I sort an array of objects by a field in those objects?

Also, so that non-edited posts sort correctly, how do I default to sort by post.date if post.date-edit isn't present in the front matter?

CodePudding user response:

It's an array filter and this should work (not tested and assuming your array name is "posts"):

{% assign posts_by_edit = posts | sort: "date-edit" %}
{% for post in posts_by_edit %}
  Do something
{% endfor %}
  • Related