Reverse loop an array of JSON data

I have a JSON array as

{
  "Education": [
    {
      "title": "Bachelor of Technology (B.Tech)",
      "current": false,
      "where": "Jawaharlal Nehru Technological University, Hyderabad",
      "from": 2007,
      "to": 2011
    },
    {
      "title": "Postgraduate Diploma in Computer and Information Sciences (PgDipCIS)",
      "current": false,
      "where": "Auckland University of Technology",
      "from": 2013,
      "to": 2014
    },
    {
      "title": "Master of Computer and Information Sciences (MCIS) (First Class Honors)",
      "current": false,
      "where": "Auckland University of Technology",
      "from": 2014,
      "to": 2015
    },
    {
      "title": "Doctor of Philosophy (PhD)",
      "current": true,
      "where": "Auckland University of Technology",
      "from": 2016,
      "to": "Present"
    }
  ]
}

And the way I am displaying it is by doing

    {{ range $k, $i := .Site.Data.Education }}
        <h2>{{$k}}</h2>
        {{ partial "education.html" . }}
    {{ end }}

and the education.html has:

<table class="uk-table uk-table-responsive">
    <tbody>
        {{ range . }}
            <tr>
                <td>{{.from}} - {{.to}}</td>
                <td>
                    {{.title}}
                    <p>{{if .company }}{{.company}},{{end}} {{.where}}</p>
                </td>
            </tr>
        {{ end }}
    </tbody>
</table>

How should I reverse the array output such that Doctor of Philosophy (PhD) is shown first? I tried using reverse I don’t think it works on maps. I also tried using Scratch to set the primary index to the length of the array and decrementing it till the last index of the array.

I don’t know what else to do, does Hugo support reversing dictionary arrays?

Hi,

Hugo has a sort function: https://gohugo.io/functions/sort/ which you could use to sort by field from.

I have tried that I get execute of template failed: template: partials/education.html:3:17: executing "partials/education.html" at <sort "from">: error calling sort: can't sort string

    {{ range sort "from" }}
        <tr>
            <td>{{.from}} - {{.to}}</td>
            <td>
                {{.title}}
                <p>{{if .company }}{{.company}},{{end}} {{.where}}</p>
            </td>
        </tr>
    {{ end }}

You need to sort the array, using from as the sortByField do something like the last example on the docs I linked to above,

untested

{{ range sort . "from" "desc" }}
  {{ .from }} ...

Thanks. It worked.