How to Reverse Order of an Array in Shopify

Liquid template is indeed useful because you can do a lot of things to show tags or items on your product page. If you want to reverse the order of an array in liquid, the tutorial will help you. The instruction below will show simple steps to reverse the order of an array.

Table of content

What is Liquid template language?

Liquid is used as the backbone of Shopify to upload dynamic content on the front page. Liquid has been sold since 2006 and now become a good source for a lot of hosted website application such as Jekyll, salesforce desk, and zendesk.

Liquid is a flexible and safe language and used in many different environments. Not only is it used on Shopify platform but Liquid is also used extensively on Jekyll websites. We are focusing on Liquid utilized on Shopify. Shopify always uses the lastest version of Liquid as a base, but Shopify inserts a significant number of objects, tags, and filters to Liquid for use in merchants’ stores.

What are array filter and reverse in liquid?

Array filter helps to create a new array from a provided array including names or items which meet the demand of the condition set by the argument function. In general, the formation of an array looks like this:

sample array filter

So, what is the argument function? The function of argument is to check the condition of each array. There are three arguments. First, array is mentioned above. Secind, index is processed by the function and element is processed by the curent element.

Moreover, the return value is also worth mentioning. The return value aims to return a new array consisting of elements that satisfy with the condition of argument filter.

Reverse in liquid is a function used to reverse the order of an array. The function of reverse can help to change the order so that the chain can appear on the front page in writer’s desire.

How to reverse the order

Take a look at this example, four types of fruits are listed below including apple, orange, peach and plum. If you want to alter the order, follow the code:

Input:


{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}

{{ my_array | reverse | join: ", " }}

Then the new order is appeared:

Output

plums, peaches, oranges, apples

Conclusion

In conclusion, using map may make the shopping journey become easier. A new tag will appear in a chain on your product page. Buyers will find it more convenient when shopping around because they do not need much time to seek key tags or information.