Can we SORT collections using SORT_BY in Liquid?!

Topic summary

A developer attempted to sort Shopify collection products using sort_by in Liquid templates but discovered this approach doesn’t work as expected.

Key Findings:

  • sort_by and default_sort_by are read-only properties in Liquid, not methods for sorting arrays
  • Liquid is a template engine (DSL) that reads data but cannot modify backend behavior or underlying objects
  • Collection sorting is controlled by: backend collection settings (merchant/app/API) and frontend URL parameters (?sort_by)

Working Solution:
Use Liquid’s array filters instead:

  • Price ascending: | sort: 'price'
  • Price descending: | sort: 'price' | reverse
  • Title sorting: | sort: 'title' (with optional | reverse)
  • Important: Filters cannot be used directly inside for loops; must pre-assign to a variable first

For Section Settings:
Creating a dropdown menu for custom sorting requires lengthy conditional statements mapping user selections to appropriate filter combinations, rather than a simple variable assignment.

Unanswered: One participant asked how to specify manual sort order via URL parameters, but received no response.

Summarized with AI on October 31. AI used: claude-sonnet-4-5-20250929.

@cfc_webmaster

I have try the code but it was not working as of now do you have any further clarification?

this code for the getting the product from the collection
option 1
{%- for product in collection.products |'sort_by=inventory' -%}

option 2
{%- for product in collection.products | sort: 'available' -%}

Error I am getting into the theme kit CLI.

main-collection.liquid) Liquid syntax error (line 217): Expected end_of_string but found pipe in “product in collection.products | sort: ‘available’”