Shopify / Shopify/liquid

Cannot Sort Metaobject by Numerical Metafield - "cannot sort values of incompatible types" Error

Open
#1,742 2 comments 4 reactions 0 assignees View on GitHub

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:
  1. Define a Metaobject with a metafield order_index of type number_integer.
  2. As shown in the code sample, Try to sort the Metaobject using Liquid syntax.
  3. 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_field or string.

Thank you for taking the time to look into this issue. I would love to hear any insights or solutions.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.