{"record":{"id":"a58ac6bbd60da452","repo":"Shopify/liquid","slug":"cannot-sort-values-of-incompatible-types","errorCode":null,"errorMessage":"cannot sort values of incompatible types","messagePattern":"cannot sort values of incompatible types","errorType":"exception","errorClass":"Liquid::ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/liquid/standardfilters.rb","lineNumber":1044,"sourceCode":"      raise Liquid::ArgumentError, \"cannot select the property '#{Utils.to_s(property)}'\"\n    end\n\n    def apply_operation(input, operand, operation)\n      result = Utils.to_number(input).send(operation, Utils.to_number(operand))\n      result.is_a?(BigDecimal) ? result.to_f : result\n    end\n\n    def nil_safe_compare(a, b)\n      result = a <=> b\n\n      if result\n        result\n      elsif a.nil?\n        1\n      elsif b.nil?\n        -1\n      else\n        raise Liquid::ArgumentError, \"cannot sort values of incompatible types\"\n      end\n    end\n\n    def nil_safe_casecmp(a, b)\n      if !a.nil? && !b.nil?\n        a.to_s.casecmp(b.to_s)\n      elsif a.nil? && b.nil?\n        0\n      else\n        a.nil? ? 1 : -1\n      end\n    end\n\n    class InputIterator\n      include Enumerable\n\n      def initialize(input, context)\n        @context = context","sourceCodeStart":1026,"sourceCodeEnd":1062,"githubUrl":"https://github.com/Shopify/liquid/blob/807d45a6b3d4568e64e86b375e3702df2c7c860c/lib/liquid/standardfilters.rb#L1026-L1062","documentation":"`nil_safe_compare` raises Liquid::ArgumentError \"cannot sort values of incompatible types\" when `sort` encounters two non-nil values that cannot be compared (e.g. a String vs an Integer). Ruby's `<=>` returns nil for such pairs, and Liquid surfaces this as a clear argument error rather than an opaque comparison failure.","triggerScenarios":"`{{ array | sort: 'key' }}` or `{{ array | sort }}` where the collection (or the values at the given property) mixes types — e.g. some items have Integer prices and others String prices.","commonSituations":"Merged data from different sources with inconsistent field types; JSON numbers serialized as strings in some records; user-supplied input bound into the array; nil coexisting with mixed typed values after record updates.","solutions":["Normalize the field to one type before sorting (e.g. convert all values to strings, or all to numbers).","Pre-process in code: coerce the collection's values to a uniform type before passing to the template.","Use `sort_natural` if case-insensitive string comparison of stringified values is acceptable.","Rescue Liquid::ArgumentError in the render error handler and fall back to unsorted rendering.","Fix upstream serialization so numeric fields are always numeric (e.g. consistent JSON typing)."],"exampleFix":"// before\n{{ items | sort: 'price' }}\n// after\n// normalize in Ruby before render: items.each { |i| i['price'] = i['price'].to_i }\n{{ items | sort: 'price' }}","handlingStrategy":"validation","validationCode":"# before render\nraise \"mixed types in 'price'\" if array.map { |i| i['price'].class }.uniq.size > 1","typeGuard":"def uniformly_typed?(arr, prop)\n  arr.map { |i| i[prop].class }.uniq.size <= 1\nend","tryCatchPattern":"begin\n  template.render(assigns)\nrescue Liquid::ArgumentError => e\n  render_unsorted_fallback\nend","preventionTips":["Normalize field types before rendering (to_i/to_s consistently)","Enforce consistent JSON serialization types upstream","Prefer sort_natural for mixed-but-stringifyable data","Test sort filters against data merged from multiple sources"],"tags":["liquid","argument-error","sorting","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"807d45a6b3d4568e64e86b375e3702df2c7c860c","analyzedAt":"2026-09-08T11:31:38.917Z","contentChangedAt":"2026-09-08T11:31:38.917Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}