Hugo sort json

Sun, Oct 9, 2022 One-minute read

Hugo sort json

Sorting JSON in hugo is quite easy.

Take this JSON object

[ 
  {
        "name": "three",
        "date": "7/02/2022",
    },
    {
       "name": "one",
       "date": "5/01/2022",
   },
    {
        "name": "two",
        "date": "7/01/2022",
    }
]

To sort this by date in Hugo you can use sort. In the HTML page to sort by desc;

{{ $items := getJSON "sort.json" }}
{{ range $item := sort $items "date" "desc" }}
<p>{{ $item.name}}</p>
<p>{{ $item.date }}</p>
{{ end }}

Tags:

#hugo #json #web