| 1 |
--- |
| 2 |
title: Alphabetical |
| 3 |
description: Returns an ordered taxonomy, sorted alphabetically by term. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: page.OrderedTaxonomy |
| 9 |
signatures: [TAXONOMY.Alphabetical] |
| 10 |
--- |
| 11 |
|
| 12 |
The `Alphabetical` method on a `Taxonomy` object returns an [ordered taxonomy](g), sorted alphabetically by [term](g). |
| 13 |
|
| 14 |
While a `Taxonomy` object is a [map](g), an ordered taxonomy is a [slice](g), where each element is an object that contains the term and a slice of its [weighted pages](g). |
| 15 |
|
| 16 |
{{% include "/_common/methods/taxonomy/get-a-taxonomy-object.md" %}} |
| 17 |
|
| 18 |
## Get the ordered taxonomy |
| 19 |
|
| 20 |
Now that we have captured the “genres” Taxonomy object, let's get the ordered taxonomy sorted alphabetically by term: |
| 21 |
|
| 22 |
```go-html-template |
| 23 |
{{ $taxonomyObject.Alphabetical }} |
| 24 |
``` |
| 25 |
|
| 26 |
To reverse the sort order: |
| 27 |
|
| 28 |
```go-html-template |
| 29 |
{{ $taxonomyObject.Alphabetical.Reverse }} |
| 30 |
``` |
| 31 |
|
| 32 |
To inspect the data structure: |
| 33 |
|
| 34 |
```go-html-template |
| 35 |
<pre>{{ debug.Dump $taxonomyObject.Alphabetical }}</pre> |
| 36 |
``` |
| 37 |
|
| 38 |
{{% include "/_common/methods/taxonomy/ordered-taxonomy-element-methods.md" %}} |
| 39 |
|
| 40 |
## Example |
| 41 |
|
| 42 |
With this template: |
| 43 |
|
| 44 |
```go-html-template |
| 45 |
{{ range $taxonomyObject.Alphabetical }} |
| 46 |
<h2><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a> ({{ .Count }})</h2> |
| 47 |
<ul> |
| 48 |
{{ range .Pages.ByTitle }} |
| 49 |
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li> |
| 50 |
{{ end }} |
| 51 |
</ul> |
| 52 |
{{ end }} |
| 53 |
``` |
| 54 |
|
| 55 |
Hugo renders: |
| 56 |
|
| 57 |
```html |
| 58 |
<h2><a href="/genres/romance/">romance</a> (2)</h2> |
| 59 |
<ul> |
| 60 |
<li><a href="/books/jamaica-inn/">Jamaica inn</a></li> |
| 61 |
<li><a href="/books/pride-and-prejudice/">Pride and prejudice</a></li> |
| 62 |
</ul> |
| 63 |
<h2><a href="/genres/suspense/">suspense</a> (3)</h2> |
| 64 |
<ul> |
| 65 |
<li><a href="/books/and-then-there-were-none/">And then there were none</a></li> |
| 66 |
<li><a href="/books/death-on-the-nile/">Death on the nile</a></li> |
| 67 |
<li><a href="/books/jamaica-inn/">Jamaica inn</a></li> |
| 68 |
</ul> |
| 69 |
``` |