mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-29 15:15:19 -07:00
@@ -122,6 +122,27 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
# Sort elements of an array ignoring case if strings
|
||||
# provide optional property with which to sort an array of hashes or drops
|
||||
def sort_natural(input, property = nil)
|
||||
ary = InputIterator.new(input)
|
||||
|
||||
# Quick function that returns the downcased object if it has a downcase,
|
||||
# otherwise it returns the object itself.
|
||||
insensitive = lambda do |obj|
|
||||
obj = obj.downcase if obj.respond_to? :downcase
|
||||
obj
|
||||
end
|
||||
|
||||
if property.nil?
|
||||
ary.sort {|a,b| insensitive.call(a) <=> insensitive.call(b) }
|
||||
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
|
||||
ary.sort {|a,b| insensitive.call(a[property]) <=> insensitive.call(b[property]) }
|
||||
elsif ary.first.respond_to?(property)
|
||||
ary.sort {|a,b| insensitive.call(a.send(property)) <=> insensitive.call(b.send(property)) }
|
||||
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