How to sort a collection ignoring "the" and "a"

I have a collection in my Jekyll site that I’d like to sort by name but ignoring articles like “the” and “a”. So, “The Cubicle” should go between “Cube” and “Dog”. If you know a magic way to do this that is different from my idea below, please suggest it.

My idea is to just add a front matter property to everything in the collection that would control the sort order, so I could keep “The Cubicle” for the name and have “Cubicle, The” for the new property (call it “sort name” or something). Is there a way I can save myself time and have the new property default to the same thing as name if I leave it out?

jekyll 4 (not on GH pages) has some sorting support, but not what you are asking for, just a way to specify how they sort which could be manageable for a small number of pages.

1 Like

I think what you want to do is create a strip_article filter that you’d call before sort, but whether you want to create a custom plugin is another question.

See Filters | Jekyll • Simple, blog-aware, static sites

Something like this might work:

module Jekyll
  module ArticleFilter
    def strip_article(input)
      input.sub(/^(an? |the )\s*/i,"")

1 Like

You can either:

  • Write a Jekyll plugin which includes a pre_render hook and adds that second variable dynamically for every post.
  • create a script that writes that second variable in the front matter.