Sorting
Sorting determines the order of rows in a report's resulting table. Sorting by one or more fields is supported.
Basic syntax
Sorting is defined as an array of objects in the sorting parameter:
{
"sorting": [
{"field": "cost", "direction": "desc"},
{"field": "campaign_name", "direction": "asc"}
]
}Sorting parameters
| Parameter | Type | Description |
|---|---|---|
field | string | The field name: a dimension or a metric |
direction | string | Direction: asc (ascending) or desc (descending) |
Directions
| Direction | Behavior for numbers | Behavior for strings | Behavior for dates |
|---|---|---|---|
asc | Smallest to largest | A → Z, a → z | Earliest to latest |
desc | Largest to smallest | Z → A, z → a | Latest to earliest |
Multi-field sorting
When multiple fields are specified, sorting is applied sequentially:
{
"sorting": [
{"field": "date", "direction": "desc"},
{"field": "cost", "direction": "desc"}
]
}Rows are first sorted by date (newest on top), then within the same date — by cost (largest on top).
Default sorting
If sorting isn't specified, the default sorting applies:
- If there's a
datedimension: by datedesc - If there are dimensions without a date: by the first dimension
asc - If there are no dimensions: no sorting (source order)
Special cases
NULL values
NULL values always come last regardless of sort direction:
asc: values → NULLdesc: values → NULL
Calculated metrics
Sorting by calculated metrics is supported:
{
"calculated_metrics": [
{"name": "cpl", "formula": "[cost] / [leads]"}
],
"sorting": [
{"field": "cpl", "direction": "asc"}
]
}Monetary values
Sorting by monetary metrics (money) is done by the numeric value in the source's currency, not by the formatted string.
Case sensitivity
String sorting is case-insensitive: "A" and "a" are treated as equal.
Limitations
- A maximum of 3 sort fields per report
- You can't sort by fields that aren't included in the report