---
_comment: Do not remove front matter.
---
Hugo determines the _next_ and _previous_ page by sorting the current section's regular pages according to this sorting hierarchy:
Field|Precedence|Sort direction
:--|:--|:--
[`weight`][]|1|descending
[`date`][]|2|descending
[`linkTitle`][]|3|descending
[`path`][]|4|descending
The sorted page collection used to determine the _next_ and _previous_ page is independent of other page collections, which may lead to unexpected behavior.
For example, with this content structure:
```tree
content/
├── pages/
│ ├── _index.md
│ ├── page-1.md <-- front matter: weight = 10
│ ├── page-2.md <-- front matter: weight = 20
│ └── page-3.md <-- front matter: weight = 30
└── _index.md
```
And these templates:
```go-html-template {file="layouts/section.html"}
{{ range .Pages.ByWeight }}
{{ end }}
```
```go-html-template {file="layouts/page.html"}
{{ with .PrevInSection }}
Previous
{{ end }}
{{ with .NextInSection }}
Next
{{ end }}
```
When you visit page-2:
- The `PrevInSection` method points to page-3
- The `NextInSection` method points to page-1
To reverse the meaning of _next_ and _previous_ you can change the sort direction in your [project configuration][], or use the [`Next`][] and [`Prev`][] methods on a `Pages` object for more flexibility.
## Example
Code defensively by checking for page existence:
```go-html-template
{{ with .PrevInSection }}
Previous
{{ end }}
{{ with .NextInSection }}
Next
{{ end }}
```
## Alternative
Use the [`Next`][] and [`Prev`][] methods on a `Pages` object for more flexibility.
[`Next`]: /methods/pages/next/
[`Prev`]: /methods/pages/prev/
[`date`]: /methods/page/date/
[`linkTitle`]: /methods/page/linktitle/
[`path`]: /methods/page/path/
[`weight`]: /methods/page/weight/
[project configuration]: /configuration/page/