'Nunjucks nested for loop sort

I am new to nunjucks.

I am trying to sort by a value in the nested for loop. As the sort can only happen in the parent for loop I am unsure how to achieve this. I can sort using a key in the parent for loop but I am not sure how to use a key from the nested for loop in the sort in the parent for loop. (Confusing I know)

   {% for stats in countries | sort(true, false, config.sortBy) %}
        <tr class="{{ "bright" if config.highlightCountry === stats.countryInfo.iso3 }}">
            <td align="left"><img src="{{ stats.countryInfo.flag }}"  width=30px height=30px/></td>
            <td align="center">{{ stats.country }}</td>
            {% for keys, data in stats.casesInfo %}
                <td align="center">{{ data }}</td>
            {% endfor %}
        </tr>
    {% endfor %}

The data structure looks something the below. So from the data structure below I want to sort by a key in the casesInfo.

myData.push({
            "updated": formatedDate,
            "country": e.country,
            "countryInfo": {
                "iso3": e.countryInfo.iso3,
                "flag": e.countryInfo.flag
            },
            "casesInfo": {
                "cases": e.cases,
                "todayCases": e.todayCases,
                "deaths": e.deaths,
                "todayDeaths": e.todayDeaths,
                "recovered": e.recovered,
                "todayRecovered": e.todayRecovered,
                "active": e.active,
                "critical": e.critical
            }

Is there anyway I can achieve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source