Order by custom metafield

I have created a custom metafield for collections with the type of Integer in admin. I am trying list the collection names and order them by the new custom metafield. Unfortunately the sorting does’t apply. Am I missing something?

{% assign sorted_collections = collections | sort: ‘metafields.custom.menu_sort.value’ %}
{% for collection in sorted_collections %}

{{ collection.title }} - {{collection.metafields.custom.menu_sort.value }}
{% endfor %}

Hi @kelliott ,

Shopify doesn’t support this one. You can refer code below:

{% assign sorted_collections = '' %}
{% for collection in collections %}
    {%  assign colTitle = collection.title  %}
    {%  assign colUrl = collection.url  %}
    {% assign sorted_collections = sorted_collections | append: collection.metafields.custom.menu_sort.value | append: "," | append: colTitle | append: "," | append: colUrl | append: ";"  %}
{% endfor %}
{%  assign  arrCollection = sorted_collections | split: ";"  | sort_natural %}
{% for col in arrCollection %}
    {%  assign collectionArr  = col | split: ","  %}
    
        {{ collectionArr[1] }}
    

{% endfor %}
1 Like

Thank you so much @EBOOST ! This worked perfectly!