How to sort line items by item's properties with liquid

Topic summary

A developer is trying to sort cart line items based on a custom property called ‘In Room’ using Liquid templating. The property contains string values like ‘Main Bathroom’ or ‘Ensuite’.

Attempted solutions (both unsuccessful):

  • {% assign sorted_line_items = cart.items | sort: "properties.In Room" %}
  • {% assign sorted_line_items = cart.items | sort: "properties['In Room']" %}

Neither approach applies any sorting. An additional complication is that not all line items have this property assigned.

The question remains unanswered, with the developer seeking suggestions for how to properly sort line items by custom property values in Liquid.

Summarized with AI on November 24. AI used: claude-sonnet-4-5-20250929.

How can I sort an array of line items (either from the cart, checkout or order objects) according to a property value that the item may have?

I’ve tried:

{% assign sorted_line_items = cart.items | sort: “properties.In Room” %}
and

{% assign sorted_line_items = cart.items | sort: “properties[‘In Room’]” %}

But neither of them work. Simply no sort is applied.

The ‘In Room’ key has a string value e.g. 'Main Bathroom, ‘Ensuite’ etc.

Some line items also may not have this property at all.

Any suggestions?

Anyone?

1 Like