Custom sort a collection, which may need to be resorted later

I am creating a Books page in Jekyll, where each book is a .md file in a _books directory, and a books.md page that displays each book in a table using the following control structure:

{% assign books = site.books | sort: ‘priority’ %}
{% for book in books %}

I’d like to display the books in a custom sort order. Adding the order in _config.yml does not appear feasible as I may have a hundred books on the page. I can add a “priority” variable in the front matter and sort on that, as shown above. However, I am hesitant to use this method in the event that I’d like to change the priority of a book in the future.

Is there a way I can maintain a custom sort, where it is also feasible to easily alter the ordering of any given book (e.g., move book from index position 10 to position 2 while bumping down all those that were previously at position 2 and below)?

Or is my best bet to write another script that goes through all the files in the _books folder and decrements the priority variable for every book between the old and new priorities for a given book?

I am using Michael Rose’s Minimal Mistakes theme in case that helps.

Thank you!

Could you use sparse data for your priority value and then do mixed sorting approach?

For example you can list just a few top priority books in config, or set priority value in frontmatter for those few. Then the top of the page can be the books which have a priority order.
And then below that show the remaining books, sorted by name or date or similar.

Alternatively, if you want to keep precise order for 100 books then put them in your config or a _data/book.yml file in a list.

If you want you can take the frontmatter from each file and migrate it to exist as key value pair in the same data list. That way the ordering of the books and their data moves together, so you don’t have the risk of them going out of sync.

1 Like

were you actually able to sort the collection by sort: 'priority?

I am trying to do the exact same thing and getting an error.

EDIT - never mind, I was using sort as my variable name which apparently is a reserved word for collection.