Sorting collection items by custom front matter

I’m trying to sort a collection by custom front matter but am getting an error - Liquid Exception: stack level too deep in index.html.

I have collection called procedures, works fine without the sort.

I have front matter in each collection item like: sort: 1

then I am looping thru them like:

{% assign sorted = site.procedures | sort: 'sort' %}
{% for item in sorted %}
    <p><a href="{{item.url}}"><h3>{{item.title}}</h3></a><p> 
{% endfor %}

If I copy those collection files and put them in a test folder - not a collection, and change the above code in the assign part to site.pages it works properly.

But as a collection it does not work.

If I change the sort from my custom variable to title it works fine.

It just won’t work on a custom variable on a collection.

This post: Custom sort a collection, which may need to be resorted later
seems to say he was able to do what I want, but maybe he never actually did it?

Using jekyll 4.0.0

1 Like

Hey @rdyar! You probably already tried this and it’s very unlikely to be the root cause, but just in case, I came across a similarly weird and obscure issue a few years ago and in that case it turned out to be a naming conflict.

So I might do a quick test by changing your front matter variable from sort to foo to quickly isolate that as the source.

Curious how this turns out!

1 Like

I just figured that out - sort apparently is a reserved thing for collection. I changed it to order and now it is doing what I want.

1 Like

This is an undocumented issue that may get fixed in a future release. But as of v4.1.0, this behavior is applicable to both pages and collection documents.

One will encounter this issue if they iterate through the same type of object as the container.

  • site.pages | sort 'sort' inside a standalone page say, or
  • site.posts | sort 'sort' inside a post or
  • | sort 'sort' inside a document within collection recipes .

The behavior will manifest for the following parameter values: 'sort' , 'min' , 'max' .

1 Like