# Sorting collection items by custom front matter

**URL:** <https://talk.jekyllrb.com/t/sorting-collection-items-by-custom-front-matter/4529>\
**Category:** Uncategorized\
**Created:** [June 3, 2020, 7:59pm UTC](https://talk.jekyllrb.com/t/sorting-collection-items-by-custom-front-matter/4529 "2020-06-03T19:59:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rdyar](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rdyar/32/710_2.png) [@rdyar](https://talk.jekyllrb.com/u/rdyar)\
**Post date:** [June 3, 2020, 7:59pm UTC](https://talk.jekyllrb.com/t/sorting-collection-items-by-custom-front-matter/4529/1 "2020-06-03T19:59:45Z")

</div>

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:

```auto
{% 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](https://talk.jekyllrb.com/t/custom-sort-a-collection-which-may-need-to-be-resorted-later/3899)  
seems to say he was able to do what I want, but maybe he never actually did it?

Using jekyll 4.0.0

---

<div class="post-metadata">

**Author:** ![ckruse](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/ckruse/32/16_2.png) [@ckruse](https://talk.jekyllrb.com/u/ckruse)\
**Post date:** [June 3, 2020, 8:11pm UTC](https://talk.jekyllrb.com/t/sorting-collection-items-by-custom-front-matter/4529/2 "2020-06-03T20:11:55Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![rdyar](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rdyar/32/710_2.png) [@rdyar](https://talk.jekyllrb.com/u/rdyar)\
**Post date:** [June 3, 2020, 8:15pm UTC](https://talk.jekyllrb.com/t/sorting-collection-items-by-custom-front-matter/4529/3 "2020-06-03T20:15:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ashmaroli](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@ashmaroli](https://talk.jekyllrb.com/u/ashmaroli)\
**Post date:** [June 4, 2020, 7:33am UTC](https://talk.jekyllrb.com/t/sorting-collection-items-by-custom-front-matter/4529/4 "2020-06-04T07:33:04Z")

</div>

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.  
Examples:

- `site.pages | sort 'sort'` inside a standalone page say, `about.md` or
- `site.posts | sort 'sort'` inside a post or
- `site.recipes | sort 'sort'` inside a _document_ within collection `recipes` .

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