Sorting

You use the orderBy query parameter to sort data.

By default, data returned by the REST API is sorted by ID number in ascending order. In the November 2015 release and later, sorting allows API clients to display returned data in a chosen order.

Note:

Only collection resources, such as incidents and contacts, support sorting.

You use the GET method with the following syntax to sort data returned from collection resources:

https://your_site_interface/services/rest/connect/version/resource
?orderBy=comma-separated columns[:asc|desc]

For related examples, see:

Example: Sorting by LookupName

You can sort incidents by reference number (that is, LookupName) in descending order. Use a GET request with the following syntax:

https://mysite.example.com/services/rest/connect/v1.4/incidents?orderBy=lookupName:desc

The following data is returned:

{
    "items": [
        {
        "id": 66,
        "lookupName": "150929-000002",
        "createdTime": "2015-09-29T20:17:11Z",
        "updatedTime": "2015-09-29T20:17:11Z",
        "links": [
            {
            "rel": "canonical",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/incidents/66"
            }
            ]
        },
        {
        "id": 65,
        "lookupName": "150929-000001",
        "createdTime": "2015-09-29T17:19:40Z",
        "updatedTime": "2015-09-29T17:19:40Z",
        "links": [
            {
            "rel": "canonical",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/incidents/65"
            }
            ]
        },
        {
        "id": 64,
        "lookupName": "150929-000000",
        "createdTime": "2015-09-29T17:11:52Z",
        "updatedTime": "2015-09-29T17:11:52Z",
        "links": [
            {
            "rel": "canonical",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/incidents/64"
            }
            ]
        },
        ...
    ]
}

Note:

In this example, you can also perform the same sort by using the id field in descending order. However, it isn't true in general.

Example: Sorting Combined with Fields Query

You can combine sorting with other query parameters, such as the fields parameter used for filtering results by field.

The following GET request:

https://mysite.example.com/services/rest/connect/v1.4/countries?fields=name,abbreviation&orderBy=name

returns the following:

{
    "items": [
        {
        "id": 9,
        "name": "Australia",
        "abbreviation": "AU",
        "links": [
            {
            "rel": "canonical",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/countries/9"
            }
            ]
        },
        {
        "id": 8,
        "name": "Canada",
        "abbreviation": "CA",
        "links": [
            {
            "rel": "canonical",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/countries/8"
            }
            ]
        },
        {
        id": 12,
        "name": "France",
        "abbreviation": "FR",
        "links": [
            {
            "rel": "canonical",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/countries/12"
            }
            ]
        },
        ...
    ]
}

Example: Sorting Combined with Other Query Parameters

This is another example of combining sorting with various query parameters.

The following GET request:

https://mysite.example.com/services/rest/connect/v1.4/serviceProducts?
q=parent IS NULL&totalResults=true&orderBy=displayOrder

returns:

{
    "items": [
        {
            "id": 55,
            "lookupName": "Cars",
            "links": [
            {
                "rel": "canonical",
                "href": "https://mysite.example.com/services/rest/connect/v1.4/serviceProducts/55"
                }
            ]
        },
        {
            "id": 58,
            "lookupName": "Couches",
            "links": [
            {
                "rel": "canonical",
                "href": "https://mysite.example.com/services/rest/connect/v1.4/serviceProducts/58"
                }
            ]
        },
        {
            "id": 61,
            "lookupName": "Printers",
            "links": [
            {
                "rel": "canonical",
                "href": "https://mysite.example.com/services/rest/connect/v1.4/serviceProducts/61"
                }
            ]
        },
        {
            "id": 83,
            "lookupName": "Appliances",
            "links": [
            {
                "rel": "canonical",
                "href": "https://mysite.example.com/services/rest/connect/v1.4/serviceProducts/83"
                }
            ]
        },
    ...
    ],
    "totalResults": 34,
    "hasMore": false,
    "links": [
    {
        "rel": "alternate",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/serviceProducts",
        "mediaType": "application/schema+json"
        },
    {
        "rel": "canonical",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/serviceProducts"
        },
    {
        "rel": "describedby",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/metadata-catalog/serviceProducts"
        },
    {
        "rel": "self",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/serviceProducts"
        }
    ]
}