Method: Liquid::StandardFilters#sort

Defined in:
lib/liquid/standardfilters.rb

#sort(input, property = nil) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/liquid/standardfilters.rb', line 379

def sort(input, property = nil)
  ary = InputIterator.new(input, context)

  return [] if ary.empty?

  if property.nil?
    ary.sort do |a, b|
      nil_safe_compare(a, b)
    end
  elsif ary.all? { |el| el.respond_to?(:[]) }
    begin
      property = Utils.to_s(property)
      ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
    rescue TypeError
      raise_property_error(property)
    end
  end
end