# How to sort pages of paginator?

**URL:** <https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012>\
**Category:** support\
**Tags:** pagination\
**Created:** [June 2, 2019, 3:13pm UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012 "2019-06-02T15:13:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![g4br](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/g4br/32/8812_2.png) [@g4br](https://discourse.gohugo.io/u/g4br)\
**Post date:** [June 2, 2019, 3:13pm UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012/1 "2019-06-02T15:13:36Z")

</div>

I have a lot of pages that represent books. They have a taxonomy called “author” so I can list all the books by a specific author easily.

On the section page of books (/book/), I would like to list all the authors with the book titles, e.g.:  
Author1 - Book1  
Author1 - Book2  
Author2 - Book3  
Author3 - Book4

What I need is a sorted paginator (sorted by the author’s name and then by the book’s title). `sort .Paginator.Pages "Params.authors" "asc"` doesn’t seem to work, I have tried some other possibilities as well without success. How should one do this?

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [June 2, 2019, 6:01pm UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012/2 "2019-06-02T18:01:05Z")

</div>

I’ve never used paginator before, but if it follows the same rules as ordering normal pages, give this a whirl:

```nohighlight
{{ range .Paginator.Pages.ByParam "authors" }}

```

---

<div class="post-metadata">

**Author:** ![g4br](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/g4br/32/8812_2.png) [@g4br](https://discourse.gohugo.io/u/g4br)\
**Post date:** [June 2, 2019, 6:14pm UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012/3 "2019-06-02T18:14:11Z")

</div>

Thanks for the comment, but sadly **the code does not work** ☹  
I tried to play around with different parameters but nothing seems to change, the “initial order” is kept (.Paginator.Pages).

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [June 2, 2019, 6:36pm UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012/4 "2019-06-02T18:36:26Z")

</div>

You need to use `.Paginate` – as you need to sort them before you paginate them.

So

```auto
{{ range (.Paginate (.Pages.ByParam "authors")).Pages }}

```

Or variations of that.

---

<div class="post-metadata">

**Author:** ![g4br](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/g4br/32/8812_2.png) [@g4br](https://discourse.gohugo.io/u/g4br)\
**Post date:** [June 2, 2019, 6:43pm UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012/5 "2019-06-02T18:43:14Z")

</div>

Sorry, but still no success. 🙂 I am starting to think that it has something to do with the fact that authors is a taxonomy and the value of it is a list.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [June 2, 2019, 8:33pm UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012/6 "2019-06-02T20:33:38Z")

</div>

I suspect you need to show us the site source or something (GitHub link?) because it’s hard to guess.

---

<div class="post-metadata">

**Author:** ![g4br](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/g4br/32/8812_2.png) [@g4br](https://discourse.gohugo.io/u/g4br)\
**Post date:** [June 3, 2019, 5:13am UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012/7 "2019-06-03T05:13:13Z")

</div>

Here you are @bep :

> <https://github.com/gabre/ppek-demo/blob/master/layouts/_default/list.html>

Note that authors -\> szerzok because it is in Hungarian. (And I failed to make it “appear in links and html in Hungarian” but keep the variable names in English - [Taxonomy terms page permalink - how to change it?](https://discourse.gohugo.io/t/taxonomy-terms-page-permalink-how-to-change-it/18998))

---

<div class="post-metadata">

**Author:** ![g4br](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/g4br/32/8812_2.png) [@g4br](https://discourse.gohugo.io/u/g4br)\
**Post date:** [June 8, 2019, 9:37am UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012/8 "2019-06-08T09:37:06Z")

</div>

@zwbetz @bep Could any of you guys kindly please take a look? This is a showstopper on my side which I cannot really fix on my own.

---

<div class="post-metadata">

**Author:** ![g4br](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/g4br/32/8812_2.png) [@g4br](https://discourse.gohugo.io/u/g4br)\
**Post date:** [June 11, 2019, 9:10pm UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012/9 "2019-06-11T21:10:14Z")

</div>

1. The sort is on a section page that is a “list page”. The following layout is used:  
[https://github.com/gabre/ppek-demo/blob/master/layouts/\_default/list.html#L12](https://github.com/gabre/ppek-demo/blob/master/layouts/_default/list.html#L12)
2. This Hugo code is called, note ByParam and the “fallback” to string mode (by carrying out a string casting) if the selected parameter is not a number:  
[https://github.com/gohugoio/hugo/blob/master/resources/page/pages\_sort.go#L339](https://github.com/gohugoio/hugo/blob/master/resources/page/pages_sort.go#L339)
3. String cast in case of a **taxonomy** that is a **string slice** results in an error (ToStringE in cast library) that is then swallowed in ToString (cast library, again).  
[https://github.com/spf13/cast/blob/master/cast.go#L103](https://github.com/spf13/cast/blob/master/cast.go#L103)

@zwbetz @bep may I kindly ask you to reply whether this is intended behaviour or am I wrong somewhere? What is the solution? Dropping usage of taxonomies in my case?

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [June 11, 2019, 9:14pm UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012/10 "2019-06-11T21:14:30Z")

</div>

Am not sure, as I’m not too familiar with custom taxonomies, or paginators. Maybe someone else can help you.

---

<div class="post-metadata">

**Author:** ![pointyfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pointyfar/32/5797_2.png) [@pointyfar](https://discourse.gohugo.io/u/pointyfar)\
**Post date:** [June 14, 2019, 12:54am UTC](https://discourse.gohugo.io/t/how-to-sort-pages-of-paginator/19012/11 "2019-06-14T00:54:59Z")

</div>

Your `.Params.szerzok` is an array, which I suspect is why your sort does not work.

Try instead:

1. Order `szerzok` taxonomy: [https://gohugo.io/templates/taxonomy-templates/#taxonomy-methods](https://gohugo.io/templates/taxonomy-templates/#taxonomy-methods)
2. Get pages asociated with taxonomy
3. Paginate page list, which now has been presorted by szerzok from #1

```auto
{{ $pagslice := slice }}
{{ range (.Site.Taxonomies.szerzok.Alphabetical) }} {{/* #1 */}}
  {{range .Pages}} {{/* #2 */}}
    {{$pagslice = $pagslice | append .Page }}
  {{end}}
{{ end }}

{{ range (.Paginate $pagslice 1).Pages }} {{/* #3; .Paginate ... 1 to force pagination on small number of pages */}} 
  {{.Params.szerzok }}, {{ .Params.Title }}<br>
{{ end }}

{{ partial "pagination.html" . }}

```
