| 1 |
--- |
| 2 |
title: collections.Sort |
| 3 |
description: Returns a sorted map or slice by reordering the given collection by a key and order. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [sort] |
| 9 |
returnType: any |
| 10 |
signatures: ['collections.Sort MAP|SLICE [KEY] [ORDER]'] |
| 11 |
aliases: [/functions/sort] |
| 12 |
--- |
| 13 |
|
| 14 |
The `KEY` is optional when sorting slices in ascending order, otherwise it is required. When sorting slices, use the literal `value` in place of the `KEY`. See examples below. |
| 15 |
|
| 16 |
The `ORDER` may be either `asc` (ascending) or `desc` (descending). The default sort order is ascending. |
| 17 |
|
| 18 |
## Sort a slice |
| 19 |
|
| 20 |
The examples below assume this project configuration: |
| 21 |
|
| 22 |
{{< code-toggle file=hugo >}} |
| 23 |
[params] |
| 24 |
grades = ['b','a','c'] |
| 25 |
{{< /code-toggle >}} |
| 26 |
|
| 27 |
### Ascending order {#slice-ascending-order} |
| 28 |
|
| 29 |
Sort slice elements in ascending order using either of these constructs: |
| 30 |
|
| 31 |
```go-html-template |
| 32 |
{{ sort site.Params.grades }} → [a b c] |
| 33 |
{{ sort site.Params.grades "value" "asc" }} → [a b c] |
| 34 |
``` |
| 35 |
|
| 36 |
In the examples above, `value` is the `KEY` representing the value of the slice element. |
| 37 |
|
| 38 |
### Descending order {#slice-descending-order} |
| 39 |
|
| 40 |
Sort slice elements in descending order: |
| 41 |
|
| 42 |
```go-html-template |
| 43 |
{{ sort site.Params.grades "value" "desc" }} → [c b a] |
| 44 |
``` |
| 45 |
|
| 46 |
In the example above, `value` is the `KEY` representing the value of the slice element. |
| 47 |
|
| 48 |
## Sort a map |
| 49 |
|
| 50 |
The examples below assume this project configuration: |
| 51 |
|
| 52 |
{{< code-toggle file=hugo >}} |
| 53 |
[params.authors.a] |
| 54 |
firstName = 'Marius' |
| 55 |
lastName = 'Pontmercy' |
| 56 |
[params.authors.b] |
| 57 |
firstName = 'Victor' |
| 58 |
lastName = 'Hugo' |
| 59 |
[params.authors.c] |
| 60 |
firstName = 'Jean' |
| 61 |
lastName = 'Valjean' |
| 62 |
{{< /code-toggle >}} |
| 63 |
|
| 64 |
> [!NOTE] |
| 65 |
> When sorting maps, the `KEY` argument must be lowercase. |
| 66 |
|
| 67 |
### Ascending order {#map-ascending-order} |
| 68 |
|
| 69 |
Sort map objects in ascending order using either of these constructs: |
| 70 |
|
| 71 |
```go-html-template |
| 72 |
{{ range sort site.Params.authors "firstname" }} |
| 73 |
{{ .firstName }} |
| 74 |
{{ end }} |
| 75 |
|
| 76 |
{{ range sort site.Params.authors "firstname" "asc" }} |
| 77 |
{{ .firstName }} |
| 78 |
{{ end }} |
| 79 |
``` |
| 80 |
|
| 81 |
These produce: |
| 82 |
|
| 83 |
```text |
| 84 |
Jean Marius Victor |
| 85 |
``` |
| 86 |
|
| 87 |
### Descending order {#map-descending-order} |
| 88 |
|
| 89 |
Sort map objects in descending order: |
| 90 |
|
| 91 |
```go-html-template |
| 92 |
{{ range sort site.Params.authors "firstname" "desc" }} |
| 93 |
{{ .firstName }} |
| 94 |
{{ end }} |
| 95 |
``` |
| 96 |
|
| 97 |
This produces: |
| 98 |
|
| 99 |
```text |
| 100 |
Victor Marius Jean |
| 101 |
``` |
| 102 |
|
| 103 |
### First level key removal |
| 104 |
|
| 105 |
Hugo removes the first level keys when sorting a map. |
| 106 |
|
| 107 |
Original map: |
| 108 |
|
| 109 |
```json |
| 110 |
{ |
| 111 |
"felix": { |
| 112 |
"breed": "malicious", |
| 113 |
"type": "cat" |
| 114 |
}, |
| 115 |
"spot": { |
| 116 |
"breed": "boxer", |
| 117 |
"type": "dog" |
| 118 |
} |
| 119 |
} |
| 120 |
``` |
| 121 |
|
| 122 |
After sorting: |
| 123 |
|
| 124 |
```json |
| 125 |
[ |
| 126 |
{ |
| 127 |
"breed": "malicious", |
| 128 |
"type": "cat" |
| 129 |
}, |
| 130 |
{ |
| 131 |
"breed": "boxer", |
| 132 |
"type": "dog" |
| 133 |
} |
| 134 |
] |
| 135 |
``` |
| 136 |
|
| 137 |
## Sort a page collection |
| 138 |
|
| 139 |
> [!NOTE] |
| 140 |
> Although you can use the `sort` function to sort a page collection, Hugo provides [sorting and grouping methods][] as well. |
| 141 |
|
| 142 |
In this contrived example, sort the site's regular pages by `.Type` in descending order: |
| 143 |
|
| 144 |
```go-html-template |
| 145 |
{{ range sort site.RegularPages "Type" "desc" }} |
| 146 |
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2> |
| 147 |
{{ end }} |
| 148 |
``` |
| 149 |
|
| 150 |
[sorting and grouping methods]: /methods/pages/ |