From 59950bff8739d34d1b005822924bd91fb29a8b3f Mon Sep 17 00:00:00 2001 From: Eric Chan Date: Wed, 13 Sep 2017 01:37:40 -0400 Subject: [PATCH] Fix sort_natural on sorting with non-string values --- lib/liquid/standardfilters.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 7c18c0d7..9ebb763e 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -143,11 +143,19 @@ module Liquid ary = InputIterator.new(input) if property.nil? - ary.sort { |a, b| a.casecmp(b) } + ary.sort { |a, b| a.to_s.casecmp(b.to_s) } elsif ary.empty? # The next two cases assume a non-empty array. [] elsif ary.first.respond_to?(:[]) && !ary.first[property].nil? - ary.sort { |a, b| a[property].casecmp(b[property]) } + ary.sort do |a, b| + a = a[property] + b = b[property] + if a && b + a[property].to_s.casecmp(b[property].to_s) + else + a ? -1 : 1 + end + end end end