From 19e704f408268f037e9d612ae85adbf253583254 Mon Sep 17 00:00:00 2001 From: Anatol Broder Date: Mon, 5 May 2014 10:26:46 +0200 Subject: [PATCH] Override the sort filter --- lib/jekyll/filters.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 997a0f5d..ced9623f 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -190,6 +190,29 @@ module Jekyll input.select { |object| object[key] == value } end + # Sort an array of objects + # + # input - the object array + # key - key within each object to filter by + # nils_last - nils appear after non-nil values in the sort ordering + # + # Returns the filtered array of objects + def sort(input, key = nil, nils_last = false) + if key.nil? + input.sort + else + input.sort { |a, b| + if a[key].nil? and !b[key].nil? + nils_last ? +1 : -1 + elsif !a[key].nil? and b[key].nil? + nils_last ? -1 : +1 + else + a[key] <=> b[key] + end + } + end + end + private def time(input) case input