HTTP & JSON jsonArraySort

$jsonArraySort[]

Sorts a JSON array alphabetically or numerically in ascending or descending order.

Syntax
$jsonArraySort[key;order?]

Parameters

Parameter Type Required Default Description
key string check_circle Required The key identifying the JSON array to sort. The key must exist and contain an array.
order string Optional asc Sort direction. Can be 'asc' (ascending) or 'desc' (descending).
asc desc

Return Value

void

Modifies the array in-place by sorting its elements.

$jsonArraySort sorts the elements of a JSON array in-place. The default order is ascending (‘asc’). For alphabetical sorting, strings are compared lexicographically. For numerical sorting, values are compared as numbers. To get descending order, either pass ‘desc’ or sort ascending and then reverse with $jsonArrayReverse.

Examples

Sort alphabetically (ascending)

$jsonParse[{"names":["Zoe","Alice","Bob","Charlie"]}]
$jsonArraySort[names;asc]
Sorted: $jsonJoinArray[names;, ]
// Output: Alice, Bob, Charlie, Zoe

Sort numerically descending

$jsonParse[{"scores":[45,88,12,96,34]}]
$jsonArraySort[scores;desc]
Top score: $jsonArrayIndex[scores;0]

Default ascending sort

$jsonParse[{"letters":["c","a","b"]}]
$jsonArraySort[letters]
$jsonJoinArray[letters;, ]