ModelWorks logoModelWorks
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

OptionTypeRequiredDefaultDescription
keystringNoFor arrays of objects, the field to compare on.
orderstringNo"asc""asc" or "desc" (case-insensitive).

Example

Input

JSON
[
  { "id": 1, "createdAt": "2024-03-10" },
  { "id": 2, "createdAt": "2024-01-02" },
  { "id": 3, "createdAt": "2024-05-21" }
]

Operation

JSON
{ "type": "sort", "options": { "key": "createdAt", "order": "desc" } }

Output

JSON
[
  { "id": 3, "createdAt": "2024-05-21" },
  { "id": 1, "createdAt": "2024-03-10" },
  { "id": 2, "createdAt": "2024-01-02" }
]

In a Circuit Step

JSON
{
  "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 array if 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 as f64.
  • Strings are compared lexicographically (byte-wise), not locale-aware.
  • A key that is missing from an element resolves to null, which sorts first in ascending order.
  • order is matched case-insensitively after lowercasing — "DESC" and "desc" are equivalent.
On this page

On this page