简体   繁体   English

排序嵌套的JSON数组

[英]Sorting nested JSON array

I'm attempting to sort this JSON object: 我正在尝试对此JSON对象进行排序:

JSONObject = {
    "command": [{
        "geobox": [...],
        "jobName": "...",
        "keywords": ["..."],
        "users": ["..."]
    }, {
        "geobox": [...],
        "jobName": "...",
        "keywords": ["...", "..."],
        "users": ["...", "...", "..."]
    }],
    "type": "..."
}

It has "command" which is an array of nested json objects and "type" which I don't really care about. 它具有“命令”,它是嵌套的json对象的数组,而“类型”我并不在乎。 I want it to sort the array of nested json objects in "command" in alphabetical order based on the jobName value. 我希望它根据jobName值按字母顺序对“命令”中的嵌套json对象数组进行排序。 I tried something like this but it didn't work. 我尝试了类似的方法,但是没有用。

JSONObject.command.sort(function (a, b) {
    return JSONObject.command[a].jobName - JSONObject.command[b].jobName
});
var compareStr = function (a, b) { 
   if (a.jobName == b.jobName) 
       return 0; 
   if (a.jobName > b.jobName) 
       return 1; 
   return -1;
};
JSONObject.command.sort(compareStr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM