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 dae8e24
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/jekyll/filters.rb
Expand Up @@ -335,19 +335,26 @@ 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_info, orange_info|
apple_property = apple_info.first
orange_property = orange_info.first

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 dae8e24

Please sign in to comment.