Index on sort

Hi!

Trying to use sort with index returns the following error: error calling sort: sequence must be provided.

Using:

{{ range sort ( index $.Site.Data.cases $.Lang ) ".home.weight" }}

On Hugo Static Site Generator v0.17 BuildDate: 2016-10-07T11:46:29-03:00.

Buuuuuuuuuuuuuut, it works.

2 Likes

Isn’t really possible to use sort and index at the same time?

Despite the error alert, the site is being generated, but it breaks a build process.

Yes, but index must return a sequence (a slice, array, whatever makes sort happy) for it to be sortable, as it says.

But… But… But it’s returning the array of data files.

Explaining better.

Inside data, I have a cases directory. Inside it, a directory for every language. So:

  • /data/cases/en/*.yaml
  • /data/cases/pt/*.yaml

I’m trying to get the specific cases of the current language, limit and sort the by .home.weight. To limit, I’m using a conditional, {{ if .home.weight }}. But I can’t understand why the code is working and reporting error at the same time.

The following code gives no error, but I can’t hardcode the language.

{{ range sort $.Site.Data.cases.en ".home.weight" }}

It looks like the sort runs before the index.

Thank you for your assistance, @bep.

Having the same issue, with hugo 0.20.1, did you solve it?

used index to get the current language:

{{ $data := index .Site.Data .Site.Language.Lang }}

then ranged the data sorting with weight:

{{ range sort ($data.products.cloud) "weight" }} {{ end }}

it gives me the same error: “error calling sort: sequence must be provided”

is this a bug or am I doing something wrong?
it works fine locally but breaks the build.

1 Like

My feeling is that it’s a bug, but it’s hard to debug it.

if it’s a bug it is a nasty one :frowning:

I found out that this code gives me a build error only if used in the index.html in the root of layouts folder.
I successfully used it in every other page of the project.

In some situations, the arrays need to be inside a parent element; on others, they didn’t. I still not sure which is which.

So, @robsonsobral, @yesh, how did you fix it?
Thanks

I didn’t, unfortunately I had to change approach with a less optimal one.

never tried with the latest releases tho’, it still has the same issue?

Hi @yesh, it’s working on.
It was failing because the data file was missing for some languages.

I added the missing file to all languages folders and it worked.

But I also protected by code with {{ if }} condition.