Out now! Check out the Poll results: Do you have a Shopify store?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: for loop - Sort the output by dates? How?

for loop - Sort the output by dates? How?

46855004463
Tourist
19 0 2

So I made some metaobjects with some inputs. See images. The issue I have is that the outputs are not in order for each date. How can I make the for loop to put out each row after the dates in order? From oldest to newest date?



46855004463_0-1694261746093.png46855004463_1-1694261772224.png46855004463_2-1694261786530.png

46855004463_3-1694261838220.png

<div class="tour-list-cont">
    <h2>Rökarn on tour - Sverige Turné!</h2>
    <p style="color: #00aaff;">
        OBS! Truck 1 står mellan 1:e Sep - 1:e Okt i Örebro vid Marieberg Gallerian!<br>
        Öppettider: Mån-Fre 11:00-20:00 | Lör-Sön 11:00-18:00.
    </p>
    <p><strong>Truck 2 är på turné, se listan nedan! 🔥🔥🔥</strong></p>

    <table width="100%">
        <tbody>
            <tr>
                <td><span style="color: #ff8000;"><strong>DATUM</strong></span></td>
                <td><span style="color: #ff8000;"><strong>STAD</strong></span></td>
                <td><span style="color: #ff8000;"><strong>PLATS</strong></span></td>
                <td><span style="color: #ff8000;"><strong>TID</strong></span></td>
            </tr> 

            {% for turne_lista in shop.metaobjects.turne_lista.values %}  
                <tr>
                    <td class="tour-date">{{ turne_lista.datum }}</td>
                    <td>{{ turne_lista.stad }}</td>
                    <td>{{ turne_lista.plats }}</td>
                    <td>{{ turne_lista.tid }}</td>
                </tr>
            {% endfor %} 
        </tbody>
    </table>
</div>

 

Replies 2 (2)

Datbaas
Shopify Partner
5 0 5

Hey 46855004463, probably something like:

Remove reverse if still in the wrong order. 

{% assign sorted_turne_lista = shop.metaobjects.turne_lista.values | sort: 'datum' | reverse %}
{% for turne_lista in sorted_turne_lista %}
{# your code to display each turne_lista item goes here #}
{% endfor %}

46855004463
Tourist
19 0 2

The list is not giving any output with that. I found same online and tried it before 😕

 

46855004463_0-1694264054432.png

<div class="tour-list-cont">
    <h2>Rökarn on tour - Sverige Turné!</h2>
    <p style="color: #00aaff;">
        OBS! Truck 1 står mellan 1:e Sep - 1:e Okt i Örebro vid Marieberg Gallerian!<br>
        Öppettider: Mån-Fre 11:00-20:00 | Lör-Sön 11:00-18:00.
    </p>
    <p><strong>Truck 2 är på turné, se listan nedan! 🔥🔥🔥</strong></p>

    <table width="100%">
        <tbody>
            <tr>
                <td><span style="color: #ff8000;"><strong>DATUM</strong></span></td>
                <td><span style="color: #ff8000;"><strong>STAD</strong></span></td>
                <td><span style="color: #ff8000;"><strong>PLATS</strong></span></td>
                <td><span style="color: #ff8000;"><strong>TID</strong></span></td>
            </tr> 

            {% assign sorted_turne_lista = shop.metaobjects.turne_lista.values | sort: 'datum' | reverse %}
            {% for turne_lista in sorted_turne_lista %} 
                <tr>
                    <td class="tour-date">{{ turne_lista.datum }}</td>
                    <td>{{ turne_lista.stad }}</td>
                    <td>{{ turne_lista.plats }}</td>
                    <td>{{ turne_lista.tid }}</td>
                </tr>
            {% endfor %} 
        </tbody>
    </table>
</div>