Code: Select all
[h:'<!-- ---------------------- json.sort (array, direction, optional:json.key) OBJECT ALSO ---------------------- -->']
[h:isArray = if(json.type(arg(0) == "ARRAY"),1,0)]
[h, if( argCount() < 3 || isArray ), CODE:{
[if(argCount() > 1): direction = arg(1) ; direction = "ascending"]
[macro.return = oldFunction(json.sort(arg(0), direction))]
};{
[sorted = oldFunction(json.sort( replace("["+substring(arg(0), 1, length(arg(0))-1)+"]", '("[^"]+")\\:\\{', '{"tmpJonsSortName":\$1,'), arg(1), arg(2) ))]
[macro.return = "{"+substring(replace(sorted, '\\{"tmpJonsSortName"\\:("[^"]+"),', '\$1:{'), 1, length(replace(sorted, '\\{"tmpJonsSortName"\\:("[^"]+"),', '\$1:{'))-1)+"}"]
}]
Code: Select all
[h:defineFunction("json.sort", "json.sort@lib:jsonSortObject", 0, 1)]
Code: Select all
[sorted = oldFunction(json.sort( replace("["+substring(arg(0), 1, length(arg(0))-1)+"]", '("[^"]+")\\:\\{', '{"tmpJonsSortName":\$1,'), arg(1), arg(2) ))]
[macro.return = "{"+substring(replace(sorted, '\\{"tmpJonsSortName"\\:("[^"]+"),', '\$1:{'), 1, length(replace(sorted, '\\{"tmpJonsSortName"\\:("[^"]+"),', '\$1:{'))-1)+"}"]
For testing purposes, it contains the above code and a test function to try it out. If you use the debug console you'll see the recursive loop which shouls not happen.:
REMOVED
REsolved: here the working lib:
lib:jsonSortObject