sortingjekyllliquid

How do I sort an array of objects by a subfield?


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 arrays of 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?


Solution

  • 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 %}