Re: Sorting Items in a Metaobject

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

royn
Shopify Partner
3 0 2

How do I sort metaobject items by a field in the metaobject.  Is there a method to perform this?   Also, how are the metaobject items retrieves ... what is the default sort order?

Replies 8 (8)

magecomp
Shopify Partner
405 28 41

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.

Helping voluntarily. Please like and accept the solution if it helps. Thanks!
Our Bestseller Shopify Apps    |      Mobile App Builder by MageComp    |      Shoplock Hide Pages/Collections

Need a developer?  Just visit MageComp website
royn
Shopify Partner
3 0 2

How would I do this (sort items in a metaobject) using liquid code.  See below. I want to sort by artist.name or artist.sort_order  

 

assign artists = shop.metaobjects.artist.values

for artist in artists

   echo artist.name

endfor  

Tristan3dt
Shopify Partner
37 0 16
To sort my 'categories' array by the title, I'm using
 
assign categories = shop.metaobjects.help_faq_category.values
assign categories = categories | sort: 'title'
sc0
Shopify Partner
4 0 0

Is it possible to sort by date? I was only able to achieve sorting by name asc and desc. I tried adding a "date" field, but when I use it for sorting it gets me an empty array it seems..

assign categories = shop.metaobjects.categories.values
assign categories = categories | sort: 'name' // works
assign categories = categories | sort: 'name' | reverse // works too
assign categories = categories | sort: 'date' // doesn't work
WildFoodOutdoor
Tourist
3 0 1

I'm trying to figure this out as well... I saw in another post that the SORT filter only works on strings and not date fields. For a hack, I added a new field to my metaobject called `date_string` and then did `sort: 'date_string'` and that works fine.

phip
Shopify Partner
1 0 8

I ran into the same problem. It was annoying because it seems that the date is stored as string in the database (in the form of YYYY-MM-DD) but gets converted to a date type when read by the `sort` filter.

I was able to resolve this by using the `sort_natural` filter which seems to read the date as string instead.

 

To sort a metaobject by date use:

assign sortable_metaboject = shop.metaobjects.my_object.values
assign sorted_metaobject = sortable_metaobject | sort_natural: 'date' 

 

Chalana
Shopify Partner
1 0 0

Thank you Phip. Worked for me!

sc0
Shopify Partner
4 0 0

Can the API endpoint be called from a liquid file? Is it necessary to enable the endpoint? I can't reach it