How can I sort items by a field in a metaobject?

Topic summary

Sorting Shopify metaobject items by a chosen field, with focus on Liquid and API options.

  • API route: One reply claims you can sort via the Admin API using orderby and direction on /admin/api/metaobjects.json, and mentions a Metaobject.sort(‘field’,‘direction’) method. They also state the default order is ascending by creation date. These points weren’t further verified by others.

  • Liquid approach: Items are accessed with shop.metaobjects..values. Sorting strings works with the Liquid filter sort: ‘field’ (e.g., title/name). Code snippets are central to the discussion.

  • Sorting by date: sort: ‘date’ did not work for some. A workaround is to store a date_string field and sort by it. A more robust fix reported by multiple participants is using sort_natural: ‘date’, which treats the stored YYYY-MM-DD as a string and sorts correctly.

  • Open questions: Whether the API endpoint can be called from Liquid, and whether it needs enabling, remains unanswered.

  • Status: Partially resolved. Effective Liquid solution for date sorting via sort_natural; API-based sorting and default-order details are not conclusively settled.

Summarized with AI on December 24. AI used: gpt-5.

Hello @royn

To sort metaobject items by a field in the metaobject, you can use the orderby parameter in the query. The orderby parameter takes the name of the field that you want to sort by, and the direction that you want to sort in. For example, to sort by the title field in ascending order, you would use the following query:

https://{shop_domain}/admin/api/metaobjects.json?orderby=title&direction=asc

To sort by the title field in descending order, you would use the following query:

https://{shop_domain}/admin/api/metaobjects.json?orderby=title&direction=desc

Is there a method to perform this?

Yes, there is a method to perform this. The method is called sort(). The sort() method takes two parameters: the name of the field that you want to sort by, and the direction that you want to sort in. For example, to sort by the title field in ascending order, you would use the following code:

Metaobject.sort('title', 'asc');

To sort by the title field in descending order, you would use the following code:

Metaobject.sort('title', 'desc');

How are the metaobject items retrieved… what is the default sort order?

Metaobject items are retrieved in ascending order by their creation date. The default sort order is ascending by creation date.