sort.md (1526B)
1 --- 2 title: sort 3 # linktitle: sort 4 description: Sorts maps, arrays, and slices and returns a sorted slice. 5 date: 2017-02-01 6 publishdate: 2017-02-01 7 lastmod: 2017-02-01 8 categories: [functions] 9 menu: 10 docs: 11 parent: "functions" 12 keywords: [ordering,sorting,lists] 13 signature: [] 14 workson: [lists,taxonomies,terms,groups] 15 hugoversion: 16 relatedfuncs: [] 17 deprecated: false 18 aliases: [] 19 --- 20 21 A sorted array of map values will be returned with the keys eliminated. There are two optional arguments: `sortByField` and `sortAsc`. If left blank, sort will sort by keys (for maps) in ascending order as its default behavior. 22 23 ``` 24 --- 25 tags: ["tag3", "tag1", "tag2"] 26 --- 27 28 // Site config 29 +++ 30 [params.authors] 31 [params.authors.Joe] 32 firstName = "Joe" 33 lastName = "Bergevin" 34 [params.authors.Derek] 35 firstName = "Derek" 36 lastName = "Perkins" 37 [params.authors.Tanner] 38 firstName = "Tanner" 39 lastName = "Linsley" 40 +++ 41 ``` 42 43 ``` 44 // Sort by value, ascending (default for lists) 45 Tags: {{ range sort .Params.tags }}{{ . }} {{ end }} 46 47 → Outputs Tags: tag1 tag2 tag3 48 49 // Sort by value, descending 50 Tags: {{ range sort .Params.tags "value" "desc" }}{{ . }} {{ end }} 51 52 → Outputs Tags: tag3 tag2 tag1 53 54 // Sort by key, ascending (default for maps) 55 Authors: {{ range sort .Site.Params.authors }}{{ .firstName }} {{ end }} 56 57 → Outputs Authors: Derek Joe Tanner 58 59 // Sort by field, descending 60 Authors: {{ range sort .Site.Params.authors "lastName" "desc" }}{{ .lastName }} {{ end }} 61 62 → Outputs Authors: Perkins Linsley Bergevin 63 ```