Use item_property

This commit is contained in:
Anatol Broder 2014-05-08 07:47:43 +02:00
parent d96f39360b
commit df3c163eeb
1 changed files with 9 additions and 17 deletions

View File

@ -193,12 +193,12 @@ module Jekyll
# Sort an array of objects
#
# input - the object array
# key - key within each object to filter by
# property - property within each object to filter by
# nils ('first' | 'last') - nils appear before or after non-nil values
#
# Returns the filtered array of objects
def sort(input, key = nil, nils = "first")
if key.nil?
def sort(input, property = nil, nils = "first")
if property.nil?
input.sort
else
case
@ -213,12 +213,15 @@ module Jekyll
end
input.sort { |a, b|
if !hash_property(a, key).nil? && hash_property(b, key).nil?
a_p = item_property(a, property)
b_p = item_property(b, property)
if !a_p.nil? && b_p.nil?
- order
elsif hash_property(a, key).nil? && !hash_property(b, key).nil?
elsif a_p.nil? && !b_p.nil?
+ order
else
hash_property(a, key) <=> hash_property(b, key)
a_p <=> b_p
end
}
end
@ -250,16 +253,5 @@ module Jekyll
item[property.to_s]
end
end
def hash_property(hash, property)
return item_property(hash, property)
if hash.respond_to?('[]'.freeze)
hash[property]
elsif hash.respond_to?(property)
hash.send(property)
else
nil
end
end
end
end