mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-18 18:30:40 -07:00
Add sort_numeric filter
This commit is contained in:
@@ -151,6 +151,23 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
# Sort elements of an array in numeric order
|
||||
# provide optional property with which to sort an array of hashes or drops
|
||||
def sort_numeric(input, property = nil)
|
||||
ary = InputIterator.new(input)
|
||||
if property.nil?
|
||||
ary.sort do |a, b|
|
||||
Utils.to_number(a) <=> Utils.to_number(b)
|
||||
end
|
||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||
[]
|
||||
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
|
||||
ary.sort do |a, b|
|
||||
Utils.to_number(a[property]) <=> Utils.to_number(b[property])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Remove duplicate elements from an array
|
||||
# provide optional property with which to determine uniqueness
|
||||
def uniq(input, property = nil)
|
||||
|
||||
Reference in New Issue
Block a user