I use a modified
Wiki: json.sort() to accommodate sorting json objects. But jfrazierjr is right. You shouldn't count on json objects being ordered, because that's not a part of their function. Just be aware if things fall out of order, this could be the reason why. But my opinion is, if this is simply ordered for output's sake and you're not indexing anything, it's no big deal. Also, if it's a structure where you add elements and you wish to reorder them a json object isn't the most efficient way. I haven't actually created a json object that I needed to sort, but I still have this function laying around if I feel so inclined.
Code:
@@ @json.sort
[H: data = arg(0)]
[H, if(argCount() >= 2): direction = arg(1); direction = "a"]
[H, if(json.isEmpty(direction)): direction = "a"; direction = substring(lower(direction),0,1)]
[H, if(json.type(data) == "OBJECT"), code: {
[H: keys = oldFunction(json.fields(data,"json"),direction)]
[H: newObj = "{}"]
[H, foreach(key,keys): newObj = json.set(newObj,key,json.get(data,key))]
};{
[H: counter = 0]
[H: args = ""]
[H, foreach(arg,macro.args), code: {
[H: args = listAppend(args,"arg"+counter)]
[H: set("arg"+counter,arg(counter))]
[H: counter = counter + 1]
}]
[H: newObj = eval(strformat("oldFunction(%{args})"))]
}]
[H: macro.return = newObj]
!!