Skip to main content

Sorting

Edges and nodes

To sort results, queries should be sent via path field edges.

By default, the received data arrays are not sorted and transmitted as they were found in the system database. This output format may be inconvenient to use if there are a lot of data and you need to work with them in a certain order. For these purposes, the received data can be sorted.


Sorting argument

To sort the data, the order argument is used, which takes as its value the field by which you want to sort the received objects and the direction specified by one of two predefined values:

  • ASC - ascending order
  • DESC - descending order

Sorting is available only for those fields that have this capability explicitly specified in the schema. An explicit indication is the use by interfaces of input values of the format XSortInput, where X is the name of the interface whose data can be used for sorting.

Example from schema

"""
List of sorting options for the Instrument interface
"""
input InstrumentModelSortInput {
basicInformation: BasicInformationSortInput
currencyInformation: CurrencyInformationSortInput
tradingDetails: TradingDetailsSortInput
financialAttributes: FinancialAttributesSortInput
boardInformation: BoardInformationSortInput
additionalInformation: AdditionalInformationSortInput
}

"""
List of sorting options for the basicInformation field
"""
input BasicInformationSortInput {
symbol: SortEnumType
exchange: SortEnumType
description: SortEnumType
shortName: SortEnumType
type: SortEnumType
market: SortEnumType
}

"""
List of allowed values for sorting
"""
enum SortEnumType {
ASC
DESC
}

Sending request

Sorting can only be applied on one field at a time. To get a sorted data array, you need to add an order argument with the field to be sorted and its direction as a value:

Request example
query Instruments {
instruments(where: { basicInformation: { symbol: { contains: "SBER" } } } order: { basicInformation: { shortName: DESC } } ) {
edges {
node {
basicInformation {
symbol
...
shortName
}
}
}
}
}
Response example
{
"data": {
"instruments": {
"edges": [
{
"node": {
"basicInformation": {
"symbol": "SBERP181224PE360",
...
"shortName": "SR360CX4"
}
}
},
{
"node": {
"basicInformation": {
"symbol": "SBERP180924PE360",
...
"shortName": "SR360CU4"
}
}
},
{
"node": {
"basicInformation": {
"symbol": "SBERP190624PE360",
...
"shortName": "SR360CR4"
}
}
},
{
"node": {
"basicInformation": {
"symbol": "SBERP080524PE360",
...
"shortName": "SR360CQ4B"
}
}
},
{
"node": {
"basicInformation": {
"symbol": "SBERP300424PE360",
...
"shortName": "SR360CQ4A"
}
}
},
{
"node": {
"basicInformation": {
"symbol": "SBERP150524PE360",
...
"shortName": "SR360CQ4"
}
}
},
{
"node": {
"basicInformation": {
"symbol": "SBERP181224CE360",
...
"shortName": "SR360CL4"
}
}
},
{
"node": {
"basicInformation": {
"symbol": "SBERP180924CE360",
...
"shortName": "SR360CI4"
}
}
},
{
"node": {
"basicInformation": {
"symbol": "SBERP190624CE360",
...
"shortName": "SR360CF4"
}
}
},
{
"node": {
"basicInformation": {
"symbol": "SBERP080524CE360",
...
"shortName": "SR360CE4B"
}
}
}
]
}
}
}

What's next?

Additionally, we recommend reading the following related articles: