
Sort a collection on property of related datamodel
Hi all,
Looking at the sorting function on the BB liquid reference, we see the following example:
products = ["a", "b", "A", "B"] {% assign products = collection.products | sort: 'title' %} {% for product in products %} {{ product.title }} {% endfor %} #=> A, B, a, b
Say, this datamodel products is related to a "productPrice" datamodel. So: products has many prices.
## PRODUCTS ##
- Id
- Title
- Description
- Etc
## PRICE ##
- Id
- Product_ID
- Price
And now you want to sort the products not on title, but on price. I would expect the following sorting function:
products = ["a", "b", "A", "B"] {% assign products = collection.products | sort: 'productPrice.price' %} {% for product in products %} {{ product.title }} {% endfor %} #=> A, B, a, b
However, this does not work! Does anyone know how one can sort a collection on a property of a related datamodel?
Thank you in advance!
Hi all,
Looking at the sorting function on the BB liquid reference, we see the following example:
products = ["a", "b", "A", "B"]
{% assign products = collection.products | sort: 'title' %}
{% for product in products %}
{{ product.title }}
{% endfor %} #=> A, B, a, b
Say, this datamodel products is related to a "productPrice" datamodel. So: products has many prices.
## PRODUCTS ##
- Id
- Title
- Description
- Etc
## PRICE ##
- Id
- Product_ID
- Price
And now you want to sort the products not on title, but on price. I would expect the following sorting function:
products = ["a", "b", "A", "B"]
{% assign products = collection.products | sort: 'productPrice.price' %}
{% for product in products %}
{{ product.title }}
{% endfor %} #=> A, B, a, b
However, this does not work! Does anyone know how one can sort a collection on a property of a related datamodel?
Thank you in advance!
Login to reply