Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不确定如何将此JSON对象排序为jQuery/JavaScript_Javascript_Jquery_Json_Sorting - Fatal编程技术网

不确定如何将此JSON对象排序为jQuery/JavaScript

不确定如何将此JSON对象排序为jQuery/JavaScript,javascript,jquery,json,sorting,Javascript,Jquery,Json,Sorting,示例字符串: var output = { "myservices":[ {"name":"oozie", "hostidn": "1", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"}, {"name":"oozie", "hostidn": "2", "details":"failed pro

示例字符串:

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"}
]};
我的爆竹杰克试图这么做

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);

我实际上需要弄清楚如何用不同的方法来分类。例如按hostidn、按名称、按currstatusclass。但我想不出最好的办法。有没有一种方法可以创建一个可以任意选择的函数,或者我需要创建一组我想基于的函数,如果有,如何创建?我的上述方法不适用于我。

不要假设在具有相同对象数据的不同主题上,你会知道我如何获得最高的hostidn值,是吗?不要假设在具有相同对象数据的不同主题上,你会知道我如何获得最高的hostidn值,是吗?
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);