# Order data files by secondary parameter

**URL:** <https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407>\
**Category:** support\
**Created:** [May 1, 2017, 8:16am UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407 "2017-05-01T08:16:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grob](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@Grob](https://discourse.gohugo.io/u/Grob)\
**Post date:** [May 1, 2017, 8:16am UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407/1 "2017-05-01T08:16:37Z")

</div>

Sorry, I probably just missed something: How is it possible to sort data file output by a “secondary” parameter?

This is what I need for a huge bibliography:

```auto
{{ range .Site.Data.bibliography }}
{{ range sort . "author.family" }}
{{ .title }} …
{{ end }}
{{ end }}

```

Thank you very much in advance!

---

<div class="post-metadata">

**Author:** ![gsienkoPL](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@gsienkoPL](https://discourse.gohugo.io/u/gsienkoPL)\
**Post date:** [May 1, 2017, 9:48am UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407/2 "2017-05-01T09:48:11Z")

</div>

You can try something like this.

```auto
{{ range sort .Site.Data.bibliography ".author.family" "desc" }}
{{ .title }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![Grob](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@Grob](https://discourse.gohugo.io/u/Grob)\
**Post date:** [May 1, 2017, 10:06am UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407/3 "2017-05-01T10:06:48Z")

</div>

Thanks a lot, @gsienkoPL

Nesting ranges seems to be necessary.

Otherwise Hugo prompts an error message:

```auto
error calling sort: author is neither a struct field, a method nor a map element of type interface {}

```

even if you do not use `sort`:

```auto
can't evaluate field title in type interface {}

```

---

<div class="post-metadata">

**Author:** ![gsienkoPL](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@gsienkoPL](https://discourse.gohugo.io/u/gsienkoPL)\
**Post date:** [May 1, 2017, 10:21am UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407/4 "2017-05-01T10:21:16Z")

</div>

Can you point to your code? I have big json data which I treat with range and sort from field inside data in the manner I write in the first post without problems.

```auto
{{ $json := getJSON "data/uchwaly.json" }}
{{ range sort $json ".data" "desc" }}
do staff....
{{ end }}

```

From the error, you don’t have author field in your data.Can you show example of your data?

---

<div class="post-metadata">

**Author:** ![Grob](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@Grob](https://discourse.gohugo.io/u/Grob)\
**Post date:** [May 1, 2017, 10:39am UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407/5 "2017-05-01T10:39:35Z")

</div>

Thanks for your effort.

I exported a .bib file using

```auto
pandoc-citeproc -y bibliography.bib > bibliography.yaml

```

and then exported it to TOML.

Here is an example:

```auto
[[references]]
id = "byron1905complete"
type = "book"
title = "The complete poetical works"
publisher = "Houghton Mifflin"

  [[references.author]]
  family = "Byron"
  given = "George Gordon Byron Baron"

  [[references.issued]]
  year = "1905"

```

The file is called `bibliography.toml` located in the data folder, so that `references` seems to be obsolete in the template file.

---

<div class="post-metadata">

**Author:** ![Grob](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@Grob](https://discourse.gohugo.io/u/Grob)\
**Post date:** [May 1, 2017, 6:29pm UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407/6 "2017-05-01T18:29:10Z")

</div>

A quick update:

Unfortunately no success with `getJSON`. Again, a secondary field caused an error.

The following sorting method apparently only works with inline frontmatter, not with data files

```auto
{{ range (.Date.Pages.ByParam "author.last_name") }}
  <!-- … -->
{{ end }}

```

I am a bit clueless now.

---

<div class="post-metadata">

**Author:** ![gsienkoPL](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@gsienkoPL](https://discourse.gohugo.io/u/gsienkoPL)\
**Post date:** [May 1, 2017, 8:47pm UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407/7 "2017-05-01T20:47:46Z")

</div>

The problem here is that you have map, not array. Try to save bibliograffy as array

```auto
[id1]
id = "byron1905complete"
type = "book"
title = "The complete poetical works"
publisher = "Houghton Mifflin"
family = "Byron"
given = "George Gordon Byron Baron"
year = "1905"
[id2]
...

```

---

<div class="post-metadata">

**Author:** ![gsienkoPL](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@gsienkoPL](https://discourse.gohugo.io/u/gsienkoPL)\
**Post date:** [May 2, 2017, 10:10pm UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407/8 "2017-05-02T22:10:13Z")

</div>

I think that even with map it should be possible using [Scratch](https://gohugo.io/extras/scratch/). But simple I’m not so good in Hugo to help you with that 😕

---

<div class="post-metadata">

**Author:** ![Grob](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@Grob](https://discourse.gohugo.io/u/Grob)\
**Post date:** [May 2, 2017, 10:44pm UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407/9 "2017-05-02T22:44:40Z")

</div>

Thanks a lot, @gsienkoPL!

The bib file is really huge so that it is hardly possible to change each entry manually.

Yes, I will have a look Scratch for this use case.

As a last resort javascript could sort the output …

---

<div class="post-metadata">

**Author:** ![gsienkoPL](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@gsienkoPL](https://discourse.gohugo.io/u/gsienkoPL)\
**Post date:** [May 5, 2017, 9:15pm UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407/10 "2017-05-05T21:15:18Z")

</div>

Ok… I think is a little ugly, but it works. It will return sorted map.

Biblio data (\data\bibliography.toml)

```auto
[[references]]
id = "1"
type = "book"
title = "Book 1"
publisher = "Publisher 1"

  [[references.author]]
  family = "Author 3"
  given = "a1"

  [[references.issued]]
  year = "1905"
  
[[references]]
id = "2"
type = "book"
title = "Book 2"
publisher = "Publisher 2"

  [[references.author]]
  family = "Author 1"
  given = "a2"

  [[references.issued]]
  year = "5091"
  
[[references]]
id = "3"
type = "book"
title = "Book 3"
publisher = "Publisher 3"

  [[references.author]]
  family = "Author 2"
  given = "a3"

  [[references.issued]]
  year = "1905"

```

and the code

```auto
{{$k := .Site.Data.bibliography.references}}
{{ range $i, $v := $k }}

  {{- if $i -}}{{- end -}}
  {{$a := slice .id .title .type .publisher}}
  {{$b := .author}}
  
  {{ range $o, $p := $b }}
    {{- if $o -}}{{- end -}}
    {{$c := slice .family .given}}
    
    {{$d := union $a $c}}
    {{$d0 := index $d 0}}
    {{$d1 := index $d 1}}
    {{$d2 := index $d 2}}
    {{$d3 := index $d 3}}
    {{$d4 := index $d 4}}
    {{$d5 := index $d 5}}
        
    {{$.Scratch.SetInMap "biblio" $d4 (dict "id" $d0 "type" $d1 "family" $d4) }}     

  {{end}}

{{end}}

{{$.Scratch.GetSortedMapValues "biblio"}}

```

The clue is to range twice, build a two slice, marge it, and used $.Scratch.SetInMap and $.Scratch.GetSortedMapValues to get the map sorted. All later taken randomly, sort are made with value of $d4.

---

<div class="post-metadata">

**Author:** ![Grob](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@Grob](https://discourse.gohugo.io/u/Grob)\
**Post date:** [May 10, 2017, 6:27am UTC](https://discourse.gohugo.io/t/order-data-files-by-secondary-parameter/6407/11 "2017-05-10T06:27:29Z")

</div>

Wow! That is gorgeous. I could not have solved it that way.

Thank you very, very much for your effort!

Inline Frontmatter _can_ be sorted directly by a secondary field, Frontmatter from the data folder can’t. Maybe, it makes sense to add a pull request to GitHub for this.
