Skip to content

Sorting

Query results from API Resources can be sorted based on a field in ascending or descending order. To sort query results, provide the _sort query parameter set to the field to sort on. Prefix the field with a dash (-) to sort in descending order.

Note

The _sort query parameter is a reserved field that cannot be used in API Resource data fields.

Ascending

1
2
curl -X GET \
  https://pet-demo.machinable.io/api/dogs?_sort=age
1
2
3
4
5
6
7
8
9
import requests

url = "https://pet-demo.machinable.io/api/dogs?_sort=age"

headers = {}

response = requests.request("GET", url, headers=headers)

print(response.text)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var data = null;

var xhr = new XMLHttpRequest();

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("GET", "https://pet-demo.machinable.io/api/dogs?_sort=age");

xhr.send(data);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import (
    "fmt"
    "net/http"
    "io/ioutil"
)

func main() {

    url := "https://pet-demo.machinable.io/api/dogs?_sort=age"

    req, _ := http.NewRequest("GET", url, nil)

    res, _ := http.DefaultClient.Do(req)

    defer res.Body.Close()
    body, _ := ioutil.ReadAll(res.Body)

    fmt.Println(res)
    fmt.Println(string(body))
}

Successful response:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
< HTTP/1.1 200 OK

{
    "count": 2,
    "items": [
        {
            "_metadata": {
                "created": 1552323811,
                "creator": "anonymous",
                "creator_type": "anonymous"
            },
            "age": 2,
            "breed": "French Bulldog",
            "id": "5c8694e3a7748bb224833f51",
            "name": "Murphy"
        },
        {
            "_metadata": {
                "created": 1552333754,
                "creator": "anonymous",
                "creator_type": "anonymous"
            },
            "age": 9,
            "breed": "German Shephard",
            "id": "5c86d3dda7748bb224833f68",
            "name": "Maximilian"
        }
    ],
    "links": {
        "self": "https://pet-demo.machinable.io/api/dogs?_limit=10&_offset=0&_sort=age"
    }
}

Descending

1
2
curl -X GET \
  https://pet-demo.machinable.io/api/dogs?_sort=-age
1
2
3
4
5
6
7
8
9
import requests

url = "https://pet-demo.machinable.io/api/dogs?_sort=-age"

headers = {}

response = requests.request("GET", url, headers=headers)

print(response.text)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var data = null;

var xhr = new XMLHttpRequest();

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("GET", "https://pet-demo.machinable.io/api/dogs?_sort=-age");

xhr.send(data);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import (
    "fmt"
    "net/http"
    "io/ioutil"
)

func main() {

    url := "https://pet-demo.machinable.io/api/dogs?_sort=-age"

    req, _ := http.NewRequest("GET", url, nil)

    res, _ := http.DefaultClient.Do(req)

    defer res.Body.Close()
    body, _ := ioutil.ReadAll(res.Body)

    fmt.Println(res)
    fmt.Println(string(body))
}

Successful response:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
< HTTP/1.1 200 OK

{
    "count": 2,
    "items": [
        {
            "_metadata": {
                "created": 1552333754,
                "creator": "anonymous",
                "creator_type": "anonymous"
            },
            "age": 9,
            "breed": "German Shephard",
            "id": "5c86d3dda7748bb224833f68",
            "name": "Maximilian"
        },
        {
            "_metadata": {
                "created": 1552323811,
                "creator": "anonymous",
                "creator_type": "anonymous"
            },
            "age": 2,
            "breed": "French Bulldog",
            "id": "5c8694e3a7748bb224833f51",
            "name": "Murphy"
        }
    ],
    "links": {
        "self": "https://pet-demo.machinable.io/api/dogs?_limit=10&_offset=0&_sort=-age"
    }
}