Cannot Sort Metaobject by Numerical Metafield - "cannot sort values of incompatible types" Error
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 11.9k
- Forks
- 1.5k
- Avg merge
- 17h 55m
- Merged PRs (30d)
- 3
Description
Summary:
When trying to sort a Metaobject that contains several metafields, including order_index of type number_integer, the Liquid engine throws an error that says cannot sort values of incompatible types. The sorting works perfectly well when sorting by single_line_text_field or string, but not with a numerical value.
Details:
We have a Metaobject defined for a size guide, containing a metafield order_index of type number_integer. We are trying to sort this Metaobject by order_index but are encountering an error.
Code sample:
liquidCopy code
{% liquid
assign sizes = shop.metaobjects.standard_size.values | sort: 'order_index'
...
%}
The error seems to be originating from this line:
https://github.com/Shopify/liquid/blob/0b9318222bcc09681e52fd5b8e70262274e673bf/lib/liquid/standardfilters.rb#L923
Inside the sort function here: https://github.com/Shopify/liquid/blob/0b9318222bcc09681e52fd5b8e70262274e673bf/lib/liquid/standardfilters.rb#L355
Steps to Reproduce:
- Define a Metaobject with a metafield
order_indexof typenumber_integer. - As shown in the code sample, Try to sort the Metaobject using Liquid syntax.
- Observe the error
cannot sort values of incompatible types.
Expected Behavior:
The Metaobject should be sorted based on the order_index field without errors.
Actual Behavior:
Throws error cannot sort values of incompatible types.
Additional Information:
- This works perfectly when sorting by a
single_line_text_fieldorstring.
Thank you for taking the time to look into this issue. I would love to hear any insights or solutions.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in lib/liquid/standardfilters.rb at the sort implementation and the incompatible-type error location referenced in the report. Reproduce sorting Metaobject values by the order_index number_integer field, compare it with string-field sorting, and verify that the numeric sort completes without the reported error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- web-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100