Manual sorting of metaobject values

I have a client with a Stockists page in which they have a list of their stockists. These stockists are grouped by their location, and then displayed in alphabetical order.

This code gets the stockists and sorts them by the location so that at least they are grouped together:

  {%  assign stockists = metaobjects.stockists.values | sort: "location" %}
  
  {% for stockist in stockists %}
    
    <div>Name: {{ stockist.name }}</div>
    <div>Location: {{ stockist.location }}</div>
    
  {% endfor %}

This will output the stockists, grouped by location but only ordered alphabetically, like so:

Stockist: Auckland
Stockist: Auckland
Stockist: Christchurch
Stockist: Wellington

The problem is that Wellington is physically higher up the country than Christchurch, so I want to be able to change the output to:

Stockist: Auckland
Stockist: Auckland
Stockist: Wellington <- manually sorted above Christchurch
Stockist: Christchurch

There does not seem to be the ability to manually sort the locations and then use that as the sorting method in liquid. Something like this would be ideal:

{% assign stockists = metaobjects.stockists.values | sort: "manual_sort" %}

Has anyone else found a creative way to do this, or what am I missing? Thanks!

@Andy_Crone ,

You can add an additional field named position to the metaobject, then sort by this field. That should give you the desired output :slight_smile:

Let me know if you need any more help — I’m here!

1 Like

Hi @Andy_Crone

Just wanted to check if the approach advised above works for you?