Sample String:
var output = {
"myservices":[
{"name":"oozie", "hostidn": "1", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"},
{"name":"oozie", "hostidn": "2", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"},
{"name":"oozie", "hostidn": "3", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"},
{"name":"oozie", "hostidn": "4", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"},
{"name":"oozie", "hostidn": "5", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"},
{"name":"single-namenode", "hostidn": "2", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"}
]};
My Cracker Jack Attempt at doing it.
function sortJSONresultsByHost(a,b)
{
var objectIDA = parseInt(a.hostidn, 10)
var objectIDB = parseInt(b.hostidn, 10)
if(objectIDA === objectIDB)
{
return 0;
}
return objectIDA > objectIDB ? 1 : -1;
}
output.myservices.sort(sortJSONresultsByHost);
I actually need to figure out how to sort this various ways. Such as by hostidn, by name, by currstatusclass. But I can't figure out the best way to do it. Is there a way to make a function that could do any by choice or would I need to make a set of functions based on which i want to do, if either how? mine above is not working out for me.
function sortObjectsByKey(objects, key){
objects.sort(function() {
return function(a, b){
var objectIDA = a[key];
var objectIDB = b[key];
if (objectIDA === objectIDB) {
return 0;
}
return objectIDA > objectIDB ? 1 : -1;
};
}());
}
sortObjectsByKey(output.myservices, "hostidn");
console.log(output.myservices);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.