Accessing data from JSON data files By Price

How to place the top listing with the highest price.

data/products.json

{
  "fruits": [
  {
    "no": 1,
    "name": "Peer",
    "price": 12,
    "id_product": "1382"
  },
  {
    "no": 2,
    "name": "Watermolen",
    "price": 19,
    "id_product": "1381"
  },
  {
    "no": 3,
    "name": "Blueberry",
    "price": 13,
    "id_product": "1387"
  }
   ]
}

/themes/anythingtheme/layouts/shortcodes/list-products.html

{{ $listproduct := site.Data.products.fruits }}

<ol>
{{ range $listproduct }}
    <li>{{.name}} | {{.price}}$</li>
{{ end }}
</ol>

So the highest price of the product is always at the top of the list! any solution friend?

{{< list-products >}}

1. Watermelon | 19$
2. Blueberry | 13$
3. Peer | 12$

Thank you :smiley:

<ol>
  {{ range (sort site.Data.products.fruits "price" "desc") }}
    <li>{{ .name }} | {{ .price }}</li>
  {{ end }}
</ol>
2 Likes

Oke thanks, sir.

gohugo

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.