Skip to content

Commit

Permalink
Move Filters#sort_input to use a Schwartzian transform.
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed Sep 1, 2017
1 parent 2303aa1 commit ccee9be
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/jekyll/filters.rb
Expand Up @@ -335,19 +335,23 @@ def inspect(input)
end

private

# Sort the input Enumerable by the given property.
# If the property doesn't exist, return the sort order respective of
# which item doesn't have the property.
# We also utilize the Schwartzian transform to make this more efficient.
def sort_input(input, property, order)
input.sort do |apple, orange|
apple_property = item_property(apple, property)
orange_property = item_property(orange, property)

if !apple_property.nil? && orange_property.nil?
- order
elsif apple_property.nil? && !orange_property.nil?
+ order
else
apple_property <=> orange_property
input.map { |item| [item_property(item, property), item] }
.sort do |apple_property, orange_property|
if !apple_property.nil? && orange_property.nil?
- order
elsif apple_property.nil? && !orange_property.nil?
+ order
else
apple_property <=> orange_property
end
end
end
.map(&:last)
end

private
Expand Down

0 comments on commit ccee9be

Please sign in to comment.