diff --git a/features/embed_filters.feature b/features/embed_filters.feature index b1a420f5..889a1fc7 100644 --- a/features/embed_filters.feature +++ b/features/embed_filters.feature @@ -101,7 +101,7 @@ Feature: Embed filters And I have the following page: | layout | content | | default | Jump | - And I have a default layout that contains "{% assign sorted_pages = site.pages | sort: 'title', true %}The rule of {{ sorted_pages.size }}: {% for p in sorted_pages %}{{ p.content | strip_html | strip_newlines }}, {% endfor %}" + And I have a default layout that contains "{% assign sorted_pages = site.pages | sort: 'title', 'last' %}The rule of {{ sorted_pages.size }}: {% for p in sorted_pages %}{{ p.content | strip_html | strip_newlines }}, {% endfor %}" When I run jekyll build Then the _site directory should exist And I should see exactly "The rule of 3: Fly, Run, Jump," in "_site/bird.html" diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index ced9623f..fe438834 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -194,18 +194,18 @@ module Jekyll # # input - the object array # key - key within each object to filter by - # nils_last - nils appear after non-nil values in the sort ordering + # nils ('first' | 'last') - nils appear before or after non-nil values # # Returns the filtered array of objects - def sort(input, key = nil, nils_last = false) + def sort(input, key = nil, nils = 'first') if key.nil? input.sort else input.sort { |a, b| if a[key].nil? and !b[key].nil? - nils_last ? +1 : -1 + nils == 'first' ? -1 : +1 elsif !a[key].nil? and b[key].nil? - nils_last ? -1 : +1 + nils == 'first' ? +1 : -1 else a[key] <=> b[key] end diff --git a/site/docs/templates.md b/site/docs/templates.md index 460e4d32..dd8c8107 100644 --- a/site/docs/templates.md +++ b/site/docs/templates.md @@ -187,12 +187,13 @@ common tasks easier.
Sort
-Sort array. Optional arguments: property, nils last.
+Sort array. Optional arguments: property name; nils order (*first* or *last*).
{% raw %}{{ page.tags | sort }}{% endraw %}
- {% raw %}{{ site.pages | sort: 'title', true }}{% endraw %}
+ {% raw %}{{ site.posts | sort: 'author' }}{% endraw %}
+ {% raw %}{{ site.pages | sort: 'title', 'last' }}{% endraw %}