[英]Not sure how to sort this JSON Object jQuery/JavaScript
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.
如按hostidn,按名称,按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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.