Here is a workaround that overrides the existing json.sort.
If you want to use it:
1. download the attached lib token.
2. Drop it into your campaign,
3. save and reload.
Thats it.
Note: it has not been thoroughly tested, so check this thread for updates.
Note2: in the rare occasion that more then one sort parameter is given, the 2nd one will be ignored.
So here a working proof of concept(copy paste into chat and hit enter to check it out)
Code: Select all
<pre>
[h:jsonArray = '[ {name:"Hero", HP:10},{HP:5, name:"Wolf"},{name:"Mage", HP:20},{name:"Troll", HP:15},{name:"Eagle", HP:5} ]']
['json.sort(jsonArray, "a", "name")']
[key = "name"]
[valList = ""]
[foreach(item, jsonArray), CODE:{
[valueArray = json.get(item, key)]
[valList = listAppend(valList, valueArray)]
}]
[valList = listSort(valList, "A+")]
[tmpArray = "[]"]
[foreach(valueList, valList), CODE:{
[found = 0]
[while (!found), CODE:{
[item = json.get(jsonArray, roll.count)]
[valueArray = json.get(item, key)]
[if(valueList == valueArray), CODE:{
[found = 1]
[tmpArray = json.append(tmpArray, item)]
''
};{''}]
''
}]
''
}]
[r:maro.return = tmpArray]
and here the 'final' version (I'll leave the PoC as its easier to read but ill continue with the one below):
Code: Select all
[h:'<!-- ---------------------- json.sort (array, direction, optional:json.key) WORKAROUND ---------------------- -->']
[r, if(argCount() < 3), CODE:{
[h,if(argCount() > 1): direction = arg(1) ; direction = "ascending"]
[r:macro.return = oldFunction(arg(0), direction)]
};{
[h:jsonArray = arg(0)]
[h:direction = arg(1)]
[h:key = arg(2)]
[h:valArray = "[]"]
[h,foreach(item, jsonArray): valArray = json.append(valArray, json.get(item, key))]
[h:valArray = json.sort(valArray, direction)]
[h:tmpArray = "[]"]
[h,foreach(valueList, valArray), CODE:{
[found = 0]
[while (!found), CODE:{
[item = json.get(jsonArray, roll.count)]
[valueArray = json.get(item, key)]
[if(valueList == valueArray), CODE:{
[tmpArray = json.append(tmpArray, item)]
<!-- make sure that when a value exists multiple times that its extracted only once -->
[jsonArray = json.remove(jsonArray, roll.count)]
[found = 1]
};{}]
''
}]
''
}]
[r:maro.return = tmpArray]
[h:'']
}]
Code: Select all
[h:defineFunction("json.sort", "json.sort@lib:fixB90", 0, 1)]
Test macro
Code: Select all
[h:jsonArray = '[ {name:"Hero", HP:10},{HP:5, name:"Wolf"},{name:"Mage", HP:20},{name:"Troll", HP:15},{name:"Eagle", HP:5} ]']
<pre>
[r:json.sort(jsonArray, "a", "name")]
[r:json.sort(jsonArray, "d", "HP")]
[r:json.sort("[5,3,7,4,5,6]")]
[r:json.sort("['def','12g','b12g','abc']")]