json.sort: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
No edit summary |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 3: | Line 3: | ||
|version=1.3b51 | |version=1.3b51 | ||
|description= | |description= | ||
Used to sort JSON arrays. If the array contains only numbers, they are sorted | Used to sort JSON arrays. If the array contains only numbers, they are sorted numerically; otherwise, the values are sorted as strings alphabetically. | ||
|usage= | |usage= | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
json.sort(array) | json.sort(array) | ||
</ | </syntaxhighlight> | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
json.sort(array, direction) | json.sort(array, direction) | ||
</ | </syntaxhighlight> | ||
If you have a [[JSON Array]] that contains [[JSON Object]]s | If you have a [[JSON Array]] that contains [[JSON Object]]s | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
json.sort(array, direction, key1, ..., keyN) | json.sort(array, direction, key1, ..., keyN) | ||
</ | </syntaxhighlight> | ||
'''Parameters''' | '''Parameters''' | ||
Line 29: | Line 29: | ||
** {{code|"d"}} | ** {{code|"d"}} | ||
* {{code|key1, ..., keyN}} - The keys in the [[JSON Object]]s contained within the [[JSON Array]] used for sorting. All [[JSON Object]]s must contain these fields. | * {{code|key1, ..., keyN}} - The keys in the [[JSON Object]]s contained within the [[JSON Array]] used for sorting. All [[JSON Object]]s must contain these fields. | ||
|examples= | |examples= | ||
Sorting a [[JSON Array]] containing numbers. | Sorting a [[JSON Array]] containing numbers. | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[json.sort("[1,4,5,6,2,1,9,20,1]")] | [json.sort("[1,4,5,6,2,1,9,20,1]")] | ||
</ | </syntaxhighlight> | ||
Produces {{code|[1,1,1,2,4,5,6,9,20]}} | Produces {{code|[1,1,1,2,4,5,6,9,20]}} | ||
Sorting a [[JSON Array]] containing strings. | Sorting a [[JSON Array]] containing strings. | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[json.sort("['Hero', 'Dragon', 'Elf', 'Wolf', 'Mage', 'Eagle', 'Troll']")] | [json.sort("['Hero', 'Dragon', 'Elf', 'Wolf', 'Mage', 'Eagle', 'Troll']")] | ||
</ | </syntaxhighlight> | ||
Produces {{code|["Dragon","Eagle","Elf","Hero","Mage","Troll","Wolf"]}} | Produces {{code|["Dragon","Eagle","Elf","Hero","Mage","Troll","Wolf"]}} | ||
Sorting a mixture of numbers and strings (all will be treated as string). | Sorting a mixture of numbers and strings (all will be treated as string). | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[json.sort("['Hero', 3, 'Elf', 'Wolf', 100, 'Eagle', 'Troll']")] | [json.sort("['Hero', 3, 'Elf', 'Wolf', 100, 'Eagle', 'Troll']")] | ||
</ | </syntaxhighlight> | ||
Produces {{code|[100,3,"Eagle","Elf","Hero","Troll","Wolf"]}} | Produces {{code|[100,3,"Eagle","Elf","Hero","Troll","Wolf"]}} | ||
Sorting objects by a single string key | Sorting objects by a single string key | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[h:vals = '[ {name:"Hero", HP:10}, | [h:vals = '[ {name:"Hero", HP:10}, | ||
{name:"Wolf", HP:5}, | {name:"Wolf", HP:5}, | ||
Line 64: | Line 60: | ||
{name:"Eagle", HP:5} ]'] | {name:"Eagle", HP:5} ]'] | ||
[json.sort(vals, "a", "name")] | [json.sort(vals, "a", "name")] | ||
</ | </syntaxhighlight> | ||
Produces | Produces | ||
Line 71: | Line 67: | ||
Sorting objects by a single numeric key | Sorting objects by a single numeric key | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[h:vals = '[ {name:"Hero", HP:10}, | [h:vals = '[ {name:"Hero", HP:10}, | ||
{name:"Wolf", HP:5}, | {name:"Wolf", HP:5}, | ||
Line 78: | Line 74: | ||
{name:"Eagle", HP:5} ]'] | {name:"Eagle", HP:5} ]'] | ||
[json.sort(vals, "a", "HP")] | [json.sort(vals, "a", "HP")] | ||
</ | </syntaxhighlight> | ||
Produces | Produces | ||
{{code| | {{code|[{"name":"Wolf","HP":5},{"name":"Eagle","HP":5},{"name":"Hero","HP":10},{"name":"Troll","HP":15},{"name":"Mage","HP":20}]}} | ||
[{"name":"Wolf","HP":5},{"name":"Eagle","HP":5},{"name":"Hero","HP":10},{"name":"Troll","HP":15},{"name":"Mage","HP":20}]}} | |||
Sorting objects by a two keys, first HP then Name. | Sorting objects by a two keys, first HP then Name. | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[h:vals = '[ {name:"Hero", HP:10}, | [h:vals = '[ {name:"Hero", HP:10}, | ||
{name:"Wolf", HP:5}, | {name:"Wolf", HP:5}, | ||
Line 94: | Line 88: | ||
{name:"Eagle", HP:5} ]'] | {name:"Eagle", HP:5} ]'] | ||
[json.sort(vals, "a", "HP", "name")] | [json.sort(vals, "a", "HP", "name")] | ||
</ | </syntaxhighlight> | ||
Produces | Produces | ||
{{code| | {{code|[{"name":"Eagle","HP":5},{"name":"Wolf","HP":5},{"name":"Hero","HP":10},{"name":"Troll","HP":15},{"name":"Mage","HP":20}]}} | ||
[{"name":"Eagle","HP":5},{"name":"Wolf","HP":5},{"name":"Hero","HP":10},{"name":"Troll","HP":15},{"name":"Mage","HP":20}]}} | |||
[[Category:JSON Function]] | [[Category:JSON Function]] | ||
}} | }} |
Latest revision as of 23:59, 15 March 2023
json.sort() Function
• Introduced in version 1.3b51
Used to sort JSON arrays. If the array contains only numbers, they are sorted numerically; otherwise, the values are sorted as strings alphabetically.
Usage
json.sort(array)
json.sort(array, direction)
If you have a JSON Array that contains JSON Objects
json.sort(array, direction, key1, ..., keyN)
Parameters
array
- The JSON array to sort.direction
- Defaults to"ascending"
, acceptable values:"ascending"
"descending"
"ascend"
"descend"
"asc"
"desc"
"a"
"d"
key1, ..., keyN
- The keys in the JSON Objects contained within the JSON Array used for sorting. All JSON Objects must contain these fields.
Examples
Sorting a JSON Array containing numbers.
[json.sort("[1,4,5,6,2,1,9,20,1]")]
Produces [1,1,1,2,4,5,6,9,20]
Sorting a JSON Array containing strings.
[json.sort("['Hero', 'Dragon', 'Elf', 'Wolf', 'Mage', 'Eagle', 'Troll']")]
Produces ["Dragon","Eagle","Elf","Hero","Mage","Troll","Wolf"]
Sorting a mixture of numbers and strings (all will be treated as string).
[json.sort("['Hero', 3, 'Elf', 'Wolf', 100, 'Eagle', 'Troll']")]
Produces [100,3,"Eagle","Elf","Hero","Troll","Wolf"]
Sorting objects by a single string key
[h:vals = '[ {name:"Hero", HP:10},
{name:"Wolf", HP:5},
{name:"Mage", HP:20},
{name:"Troll", HP:15},
{name:"Eagle", HP:5} ]']
[json.sort(vals, "a", "name")]
Produces
[{"name":"Eagle","HP":5},{"name":"Hero","HP":10},{"name":"Mage","HP":20},{"name":"Troll","HP":15},{"name":"Wolf","HP":5}]
Sorting objects by a single numeric key
[h:vals = '[ {name:"Hero", HP:10},
{name:"Wolf", HP:5},
{name:"Mage", HP:20},
{name:"Troll", HP:15},
{name:"Eagle", HP:5} ]']
[json.sort(vals, "a", "HP")]
Produces
[{"name":"Wolf","HP":5},{"name":"Eagle","HP":5},{"name":"Hero","HP":10},{"name":"Troll","HP":15},{"name":"Mage","HP":20}]
Sorting objects by a two keys, first HP then Name.
[h:vals = '[ {name:"Hero", HP:10},
{name:"Wolf", HP:5},
{name:"Mage", HP:20},
{name:"Troll", HP:15},
{name:"Eagle", HP:5} ]']
[json.sort(vals, "a", "HP", "name")]
Produces
[{"name":"Eagle","HP":5},{"name":"Wolf","HP":5},{"name":"Hero","HP":10},{"name":"Troll","HP":15},{"name":"Mage","HP":20}]