Skip to content

Instantly share code, notes, and snippets.

Forked from JanDupal/
Last active Aug 29, 2015
What would you like to do?

Jekyll sorted_for plugin

Quick'n'dirty Jekyll plugin for sorted cycle.


This fork fixes two issues:

  • problems when specifiying sort fields like sort_by:'weight' (with ' or " characters)
  • problems when a collection entry does not have the specified sort field


Copy sorted_for.rb to _plugins/ directory of your Jekyll site.


Instead of for in templates use sorted_for and add sort_by parameter with property you want to sort by. Also supports reversed parameter as the original for tag.

{% sorted_for node in site.pages reversed sort_by:weight %}
  {{ node.title }}
{% endsorted_for %}

To use custom sort property (eg. weight as in example above) add it to YAML Front Matter of your pages - see

module Jekyll
class SortedForTag < Liquid::For
def render(context)
sort_by_field = @attributes['sort_by'].gsub(/("|')/, "") # remove " and ' chars like in 'orderfield'
sorted_collection = context[@collection_name].dup
sorted_collection.sort_by! { |i|
i.to_liquid[sort_by_field] || 0 # use 0 if the sort_by_field is missing
sorted_collection_name = "#{@collection_name}_sorted".sub('.', '_')
context[sorted_collection_name] = sorted_collection
@collection_name = sorted_collection_name
def end_tag
Liquid::Template.register_tag('sorted_for', Jekyll::SortedForTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment