Back to Home
$jsonArraySort
$jsonArraySort sorts a JSON array in ascending order.
Numbers are sorted from lowest to highest. Strings are sorted by their
ASCII/Unicode values, with uppercase letters coming before lowercase.
Syntax
$jsonArraySort[Key;...]
-
Key: one or more keys forming the path to the array.
What happens
$jsonParseloads the JSON object.-
$jsonArraySortsorts the array at the given key path in ascending order. -
Nothing is returned. Use
$jsonor$jsonJoinArrayto see the sorted result.
Example 1: Sort a number array
$nomention
$jsonParse[{"scores":[50,10,90,30,70\]}]
$jsonArraySort[scores]
$jsonJoinArray[scores;, ]
What happens:
$jsonParseloads the object.-
$jsonArraySort[scores]sorts the numbers ascending. $jsonJoinArraydisplays the result.
Output:
10, 30, 50, 70, 90
Example 2: Sort a mixed array
$nomention
$jsonParse[{"data":["banana",10,"apple",2,"Apples",30\]}]
$jsonArraySort[data]
$jsonJoinArray[data;, ]
What happens:
$jsonParseloads the object.-
$jsonArraySort[data]sorts it: numbers first, then strings by ASCII value (Applesbeforeapplebeforebanana). $jsonJoinArraydisplays the result.
Output:
2, 10, 30, Apples, apple, banana
Common uses
- Leaderboards: sort score arrays before displaying
- Alphabetical lists: sort string arrays like names or items
- Normalising data: sort before searching or comparing
See also
- $jsonArrayReverse: to reverse after sorting for descending order
- $jsonJoinArray: to display the sorted array as a string
- $jsonArrayCount: to check how many elements are in the array