I want to sort collection products by 'best-selling' but it's not work when i test .
i don't know why. Below is my code
{% assign products = collection.products | sort: 'best-selling' %}
When i sort it with 'price', it work, but with best-selling it's not.I hope someone can help me how to do it
The sort
function is a generic sort for array. You can sort by any field of the element in the array. In case of products you can write
{% assign products = collection.products | sort: 'title' %}
because the title (or price, etc) is an attribute of the product. A product doesn't have any attribute that specify how many units have been sold.
You can change the default sorting method of your collection to 'Best Selling' so that products are sorted that way by default.
Also, you were probably confused by the sort_by
function which is quite different
{{ collection.url | sort_by: 'best-selling' }}
gives you the link to the collection page using the given sort function.