# Runtime sorting of columns

> For the complete documentation index, see [llms.txt](https://developers.thoughtspot.com/docs/llms.txt)

Source: https://developers.thoughtspot.com/docs/runtime-sort

# Runtime sorting of columns

Runtime sorting allows applying sort criteria to a Liveboard or Answer object on load or when querying a Liveboard or Answer data via REST API calls.

## How it works

You can add runtime sort attributes as query parameters to the URL. The query parameter format of a runtime sort operation is as follows:

?sortCol1=<COL\_NAME>&asc1=<BOOL>

sortCol1

_String_. Name of the column to sort by. For example, if you want to sort the `Sales` column in ascending order, you can specify the column name as `sortCol1=Sales`.

asc1

_Boolean_. Optional. Indicates the sorting order. To sort a column in descending order, specify `asc1=false`. By default, the `asc1` value is set to `true`.

The following example shows the URL parameters to sort the `Sales` column in descending order on a Liveboard.

https://{ThoughtSpot-host}/#/pinboard/c476b285-6285-4038-87af-d69d32531e48?sortCol1=Sales&asc1=false

> **NOTE:** The runtime sort operation returns an error if the URL exceeds 2000 characters.

### Support for multiple sort parameters

You can append multiple runtime sort attributes to a Liveboard or Answer URL. Unlike runtime Filters, the runtime sort attribute-value pair must be applied in a specific order.

The following example shows the correct order of runtime sort parameters:

https://{ThoughtSpot-host}/#/pinboard/c476b285-6285-4038-87af-d69d32531e48?sortCol1=Sales&asc1=false&sortCol2=Tea&sortCol3=Revenue&asc3=false

In the above example, the `Sales` column is sorted in descending order first and then the `Tea` column is sorted alphabetically in ascending order. The `Revenue` column is sorted in descending order after the first two parameters are applied.

If you swap the sort order of these columns, the resulting output changes accordingly.

## Runtime sorting of columns via REST API

The following REST API v2 endpoints support applying runtime sort parameters:

-   `POST /api/rest/2.0/report/liveboard`
    
-   `POST /api/rest/2.0/report/answer`
    
-   `POST /api/rest/2.0/searchdata`  
    
-   `POST /api/rest/2.0/metadata/liveboard/data`  
    
-   `POST /api/rest/2.0/metadata/answer/data`  
    

The following examples show the request body with runtime sort attributes:

### Answer report

```cURL
curl -X POST \
  --url 'https://{ThoughtSpot-Host}/api/rest/2.0/report/answer' \
  -H 'Authorization: Bearer {access-token} '\
  -H 'Content-Type: application/json' \
  --data-raw '{
  "metadata_identifier": "fa68ae91-7588-4136-bacd-d71fb12dda69",
  "runtime_sort": {
    "sortCol1": "sales",
    "asc1": false,
    "sortCol2": "region",
    "asc2": true
  },
  "file_format": "CSV"
}'
```

### Liveboard report

```cURL
curl -X POST \
  --url 'https://{ThoughtSpot-Host}/api/rest/2.0/report/liveboard' \
  -H 'Authorization: Bearer {access-token} '\
  -H 'Content-Type: application/json' \
  --data-raw '{
  "metadata_identifier": "fa68ae91-7588-4136-bacd-d71fb12dda69",
  "runtime_sort": {
    "sortCol1": "sales",
    "asc1": true,
    "sortCol2": "region",
    "asc2": true
  },
  "file_format": "CSV"
}'
```

### Search data

```cURL
curl -X POST \
  --url 'https://{ThoughtSpot-Host}/api/rest/2.0/searchdata' \
  -H 'Authorization: Bearer {access-token}'\
  -H 'Accept: application/json'\
  -H 'Content-Type: application/json' \
  --data-raw '{
  "query_string": "[Revenue] [Color]",
  "logical_table_identifier": "9bd202f5-d431-44bf-9a07-b4f7be372125",
  "runtime_sort": {
    "sortCol1": "revenue",
    "asc1": true,
    "sortCol2": "color",
    "asc2": false
  }
}'
```

### Answer data

```cURL
curl -X POST \
  --url 'https://{ThoughtSpot-Host}/api/rest/2.0/metadata/answer/data' \
  -H 'Authorization: Bearer {access-token}'\
  -H 'Accept: application/json'\
  -H 'Content-Type: application/json' \
  --data-raw '{
  "metadata_identifier": "f605dbc7-db19-450b-8613-307118f74c3c",
  "runtime_sort": {
    "sortCol1": "sales",
    "asc1": true,
    "sortCol2": "region",
    "asc2": false
  }
}'
```

### Liveboard data

```cURL
curl -X POST \
  --url 'https://{ThoughtSpot-Host}/api/rest/2.0/metadata/liveboard/data' \
  -H 'Authorization: Bearer {access-token}'\
  -H 'Accept: application/json'\
  -H 'Content-Type: application/json' \
  --data-raw '{
  "metadata_identifier": "e9d54c69-d2c1-446d-9529-544759427075",
  "runtime_sort": {
    "sortCol1": "revenue",
    "asc1": true,
    "sortCol2": "color",
    "asc2": false
  }
}
```

### REST API v1 (Legacy endpoints)

If you are using REST API v1 endpoints, runtime sorts can be applied when fetching a Liveboard or Answer object from ThoughtSpot.

To sort columns when fetching data from a Liveboard object, add sort properties to the Liveboard data API request URL as shown in the example here:

http://{ThoughtSpot-host}/callosum/v1/tspublic/v1/pinboarddata?id=e36ee65e-64be-436b-a29a-22d8998c4fae&batchsize=-1&pagenumber=-1&offset=-1&formattype=COMPACT&sortCol1=Sales&asc1=false

https://{ThoughtSpot-host}/callosum/v1/tspublic/v1/pinboarddata?id=e36ee65e-64be-436b-a29a-22d8998c4fae&batchsize=-1&pagenumber=-1&offset=-1&formattype=COMPACT&sortCol1=Sales&asc1=false&sortCol2=Tea

To sort columns of an Answer object, add the sort attributes to the search data API request URL as shown in the following examples:

https://{ThoughtSpot-host}/callosum/v1/tspublic/v1/searchdata?batchsize=-1&pagenumber=-1&offset=-1&formattype=COMPACT&sortCol1=Sales&asc1=false

https://{ThoughtSpot-host}/callosum/v1/tspublic/v1/searchdata?batchsize=-1&pagenumber=-1&offset=-1&formattype=COMPACT&sortCol1=Sales&asc1=false&sortCol2=Tea