Sort array of metaobjects by value of field

Topic summary

A developer is trying to sort an array of Shopify metaobjects (brands) by a custom integer field called order before iterating through them in a Liquid template.

Current Approach:

  • Using shop.metaobjects.brands.values | sort: 'order' to sort the array
  • Each brand metaobject has an order field (integer type)
  • Goal is to sort brands by this field value before the for loop executes

Code Context:

  • The loop filters brands by category and displays brand logos with links
  • The sorting needs to happen at the assignment stage to control display order

Status: The question remains unanswered. The developer needs guidance on the correct Liquid syntax to access and sort by nested metaobject field values, as the standard sort filter may not directly support sorting by metaobject properties.

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

I’m attempting to sort my array by the value of my order field in my metaobject. For example, each brand entry has brand.order which is an integer. How can i access these values and sort them before my for loop runs?

{% assign sorted_brands = shop.metaobjects.brands.values | sort: 'order' %}
{% for brand in sorted_brands %}
  {% if brand.category == category %}
    <li class="flex items-center justify-center">
      <a href="{{ brand.url }}" target="_blank" class="max-h-[44px] w-auto flex items-center justify-center">
        <img
          srcset="{{ brand.logo.value | image_url }}"
          src="{{ brand.logo.value | image_url }}"
          alt="{{ brand.name }}"
          height="44"
          width="86"
          class="max-h-[44px] w-auto"
          loading="lazy"
        >
      </a>
    </li>
  {% endif %}
{% endfor %}