Payload is designed with performance in mind, but its customizability means that there are many ways to configure your app that can impact performance. While Payload provides several features and best practices to help you optimize your app's specific performance needs, these are not currently well surfaced and can be obscure. Now: - A high-level performance doc now exists at `/docs/performance` - There's a new section on performance within the `/docs/queries` doc - There's a new section on performance within the `/docs/hooks` doc - There's a new section on performance within the `/docs/custom-components` doc This PR also: - Restructures and elaborates on the `/docs/queries/pagination` docs - Adds a new `/docs/database/indexing` doc - More --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1210743577153856
83 lines
2.7 KiB
Plaintext
83 lines
2.7 KiB
Plaintext
---
|
|
title: Sort
|
|
label: Sort
|
|
order: 20
|
|
desc: Payload sort allows you to order your documents by a field in ascending or descending order.
|
|
keywords: query, documents, pagination, documentation, Content Management System, cms, headless, javascript, node, react, nextjs
|
|
---
|
|
|
|
Documents in Payload can be easily sorted by a specific [Field](../fields/overview). When querying Documents, you can pass the name of any top-level field, and the response will sort the Documents by that field in _ascending_ order.
|
|
|
|
If prefixed with a minus symbol ("-"), they will be sorted in _descending_ order. In Local API multiple fields can be specified by using an array of strings. In REST API multiple fields can be specified by separating fields with comma. The minus symbol can be in front of individual fields.
|
|
|
|
Because sorting is handled by the database, the field cannot be a [Virtual Field](https://payloadcms.com/blog/learn-how-virtual-fields-can-help-solve-common-cms-challenges) unless it's [linked with a relationship field](/docs/fields/relationship#linking-virtual-fields-with-relationships). It must be stored in the database to be searchable.
|
|
|
|
<Banner type="success">
|
|
**Tip:** For performance reasons, it is recommended to enable `index: true`
|
|
for the fields that will be sorted upon. [More details](../database/indexes).
|
|
</Banner>
|
|
|
|
## Local API
|
|
|
|
To sort Documents in the [Local API](../local-api/overview), you can use the `sort` option in your query:
|
|
|
|
```ts
|
|
import type { Payload } from 'payload'
|
|
|
|
const getPosts = async (payload: Payload) => {
|
|
const posts = await payload.find({
|
|
collection: 'posts',
|
|
sort: '-createdAt', // highlight-line
|
|
})
|
|
|
|
return posts
|
|
}
|
|
```
|
|
|
|
To sort by multiple fields, you can use the `sort` option with fields in an array:
|
|
|
|
```ts
|
|
import type { Payload } from 'payload'
|
|
|
|
const getPosts = async (payload: Payload) => {
|
|
const posts = await payload.find({
|
|
collection: 'posts',
|
|
sort: ['priority', '-createdAt'], // highlight-line
|
|
})
|
|
|
|
return posts
|
|
}
|
|
```
|
|
|
|
## REST API
|
|
|
|
To sort in the [REST API](../rest-api/overview), you can use the `sort` parameter in your query:
|
|
|
|
```ts
|
|
fetch('https://localhost:3000/api/posts?sort=-createdAt') // highlight-line
|
|
.then((response) => response.json())
|
|
.then((data) => console.log(data))
|
|
```
|
|
|
|
To sort by multiple fields, you can use the `sort` parameter with fields separated by comma:
|
|
|
|
```ts
|
|
fetch('https://localhost:3000/api/posts?sort=priority,-createdAt') // highlight-line
|
|
.then((response) => response.json())
|
|
.then((data) => console.log(data))
|
|
```
|
|
|
|
## GraphQL API
|
|
|
|
To sort in the [GraphQL API](../graphql/overview), you can use the `sort` parameter in your query:
|
|
|
|
```ts
|
|
query {
|
|
Posts(sort: "-createdAt") {
|
|
docs {
|
|
color
|
|
}
|
|
}
|
|
}
|
|
```
|