Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tracked by #1743
gazjones00 opened this issue Sep 5, 2023 · 2 comments

Comments

@gazjones00
Copy link

gazjones00 commented Sep 5, 2023

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:

raise Liquid::ArgumentError, "cannot sort values of incompatible types"

Inside the sort function here:

def sort(input, property = nil)

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.

@Czx00000 Czx00000 mentioned this issue Sep 12, 2023
@lucasaraujo45
Copy link

@gazjones00

I encountered the same issue when attempting to sort by date/number. I wasn't able to sort by number and the date isn't sourced from a metaobject JSON.

As a temporary workaround, I defined the date as a string within the metaobject. I then used regex validation to ensure user input adheres to the 'yyyy-mm-dd' format. By doing this, I was able to sort the dates alphabetically, and this approach successfully resolved the issue for me.

I hope this helps someone else facing a similar problem, but it'd be great if the core issue with sorting numerical types could be addressed. Thanks for this issue!

@NeuroticCoder91
Copy link

Seems entirely silly that we cannot sort by date type in a metaobject

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants