Search code examples
shopifyliquidliquid-layout

Shopify Sort cart.items array using Liquid script


I want to sort the cart.items array in the cart.liquid file. And then I can display the times in the table in my expected order.

    {% for item in cart.items sort_by:item.line_price %}
    <div class="row">
      <div class="span12">
      <h1>Your cart</h1>
      <form action="/cart" method="post" id="cartform">
        <table>
        </table>
      </form>
      </div>
    </div>
    {% endfor %}

But the code doesn't work since I can't use sort_by:item.line_price in for statement.

How can I sort an array using built-in features?

The other problem is Shopify liquid doesn't support to create an array. if I use my own algorithm to sort the array. But how can I save the output to a new array?

Thanks a lot.


Solution

  • You could just render your data to a Javascript data structure and then sort it. Use a template in Javascript. Handlebars.js works well for that. There is no built-in sort.