Skip to main content
Version: v3.x (DDN)

Sorting

Request format

GET /v1/rest/<subgraph-name>/<model-name>?sort=<fields>
ParameterDescriptionExample
subgraph-nameName of your subgraph."default"
model-nameName of your model."Users"
fieldsComma-separated list of fields, prefix with - for descending order."name", "-created_at"

Examples

Single field

Fetch all articles sorted by title in ascending order.

Request:
GET /v1/rest/default/Articles?sort=title
Response:
{
"data": [
{
"type": "Articles",
"id": "1",
"attributes": {
"title": "A First Article",
"created_at": "2023-01-02T12:00:00Z"
}
},
{
"type": "Articles",
"id": "2",
"attributes": {
"title": "B Second Article",
"created_at": "2023-01-01T12:00:00Z"
}
}
]
}

Multiple fields

Fetch all articles sorted by created date in descending order and then by title in ascending order.

Request:
GET /v1/rest/default/Articles?sort=-created_at,title
Response:
{
"data": [
{
"type": "Articles",
"id": "1",
"attributes": {
"title": "A First Article",
"created_at": "2023-01-02T12:00:00Z"
}
},
{
"type": "Articles",
"id": "3",
"attributes": {
"title": "C Third Article",
"created_at": "2023-01-02T12:00:00Z"
}
},
{
"type": "Articles",
"id": "2",
"attributes": {
"title": "B Second Article",
"created_at": "2023-01-01T12:00:00Z"
}
}
]
}

Descending field

Fetch all articles sorted by views in descending order.

Request:
GET /v1/rest/default/Articles?sort=-views
Response:
{
"data": [
{
"type": "Articles",
"id": "1",
"attributes": {
"title": "Popular Article",
"views": 2000
}
},
{
"type": "Articles",
"id": "2",
"attributes": {
"title": "Less Popular Article",
"views": 1500
}
}
]
}