Sort products by category field value All of my products have a category field called stateCat. These state categories have a field called stateImportance. This is because some states are more important than others and I want to sort the products based on that stateImportance number. I have no idea how to do this.

This is what I've tried:

{% set products = craft.commerce.products.order('stateCat.stateImportance asc').type('coins').limit(null) %}


This doesn't seem to work, how can I accomplish this?
This doesn't work out of the box with Craft as the category field is a relation field.

However you should be able to use SuperSort plugin.

{% set products = craft.commerce.products.type('coins').limit(null) %}
{% set orderedProducts = products | supersort('sortAs', '{stateCat.first.stateImportance}' %}