Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Sorting Metaobject array by date or number - Liquid

Sorting Metaobject array by date or number - Liquid

jim00
New Member
9 0 0

So I have pages created by metobjects, and am trying to create a page to index these with a bit of liquid code. All works fine - except it seems I can only sort on text fields where as I need to sort on a date eg  I have created metaobjects with field "publication_date", i then sort that as follows:

 

 

{% assign mags = mags | sort: 'publication_date' %}

 

 

Outputting with a For loop:

 

{% for myMag in mags %}
    {{myMag.publication_date }} <br>
{% endfor %}

 

Results in:

2013-03-01
2014-03-01
2014-03-01
2012-12-01
2013-12-01
2012-02-01
2014-01-01
2013-05-01
2012-11-01
2012-10-01
2013-09-01
2016-10-01
2013-11-01
2015-12-01
2016-07-01
2021-03-01
2014-12-01
2021-03-01
2014-12-01
2014-12-01
2014-01-01
2014-11-01
2012-10-01
2013-12-01
2015-02-01
2014-12-01
2014-06-01
2016-05-01
2019-02-01

 

and not the following that i'd expect/like:

2012-02-01
2012-10-01
2012-10-01
2012-11-01
2012-12-01
2013-03-01
2013-05-01
2013-09-01
2013-11-01
2013-12-01
2013-12-01
2014-01-01
2014-01-01
2014-03-01
2014-03-01
2014-06-01
2014-11-01
2014-12-01
2014-12-01
2014-12-01
2014-12-01
2015-02-01
2015-12-01
2016-05-01
2016-07-01
2016-10-01
2019-02-01
2021-03-01
2021-03-01

 

I thought it might be to do with the Sort function not interpreting the Data type - so have tried adding a different field with the dates entered as an Integer without the dashes - but result is the same. If I sort on a text field it sorts that field alphabetically without any issues - just not numbers.

 

What am I missing?!

Reply 1 (1)

jim00
New Member
9 0 0

In case anyone has similar issue -  it seems that sort() only works on text datatypes - adding a field of type "single line text", and entering the date without the number then the above gives the following expected result:

20120201
20121001
20121001
20121101
20121201
20130301
20130501
20130901
20131101
20131201
20131201
20140101
20140101
20140301
20140301
20140601
20141101
20141201
20141201
20141201
20141201
20150201
20151201
20160501
20160701
20161001
20190201
20210301
20210301

 

So it seems like a bug - and the workaround is to use a text field...frustrating!