Override the sort filter

This commit is contained in:
Anatol Broder 2014-05-05 10:26:46 +02:00
parent cb22320ae6
commit 19e704f408
1 changed files with 23 additions and 0 deletions

View File

@ -190,6 +190,29 @@ module Jekyll
input.select { |object| object[key] == value } input.select { |object| object[key] == value }
end 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 private
def time(input) def time(input)
case input case input