How to sort on inner field?

This is in and many other

  ogc_fid: 4
  name: some_name
  num: 7

I want to sort pages by ogc_fid
And this is in layout.

{{ range sort .Data.Pages "" }}

Sorting on ogc_fid doesn’t work.

Server show an error:

ERROR 2018/12/13 22:09:18 Failed to render pages: render of "section" failed: "d
.....\test\layouts\pass\pass.html:9:11": execute of template fail
ed: template: pass/pass.html:9:11: executing "pass/pass.html" at <sort .Data.Pag
es ".P...>: error calling sort: can't evaluate an invalid value

This should work fine as you have it

{{ range sort .Data.Pages "" }}

This error is likely because one of your pages doesn’t have a value for ogc_fid

No it is not. Every page has ogc_fid

Would need to see your code to help anymore. Else I’m just shooting in the dark

You were right. Pags without ogc_fid break everything
The workaround is

{{$tmp := where .Pages "" "!=" nil}}
	  {{ range sort $tmp ""}}

Thanks for helping people!

1 Like

Next time, please follow the advice at Requesting Help and share your project. if you can not, please recreate a project that reproduces your issue that can be easily shared.