JSON TransformArray
sort
Sort an array, optionally by a key
sort
Sorts an array in ascending or descending order. Without key, elements are compared directly. With key, the named field of each object element drives the comparison.
Options
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
key | string | No | — | For arrays of objects, the field to compare on. |
order | string | No | "asc" | "asc" or "desc" (case-insensitive). |
Example
Input
[
{ "id": 1, "createdAt": "2024-03-10" },
{ "id": 2, "createdAt": "2024-01-02" },
{ "id": 3, "createdAt": "2024-05-21" }
]Operation
{ "type": "sort", "options": { "key": "createdAt", "order": "desc" } }Output
[
{ "id": 3, "createdAt": "2024-05-21" },
{ "id": 1, "createdAt": "2024-03-10" },
{ "id": 2, "createdAt": "2024-01-02" }
]In a Circuit Step
{
"name": "newest_first",
"description": "Sort items by createdAt, newest first",
"function": "circuit.core.transform.json",
"input": {
"source": "${{ items }}",
"operations": [
{ "type": "sort", "options": { "key": "createdAt", "order": "desc" } }
]
},
"outputs": [
{
"name": "sorted_items",
"description": "Items ordered by createdAt descending",
"value": "${{ newest_first.output.transformed_result }}"
}
]
}Notes & gotchas
- Errors with
sort: expected arrayif the input is not an array. - Mixed-type elements fall back to a stable type ordering:
null<bool<number<string<array<object. - Numbers are compared as integers when both fit
i64, otherwise asf64. - Strings are compared lexicographically (byte-wise), not locale-aware.
- A
keythat is missing from an element resolves tonull, which sorts first in ascending order. orderis matched case-insensitively after lowercasing —"DESC"and"desc"are equivalent.