简体   繁体   中英

Sorting nested JSON array

I'm attempting to sort this JSON object:

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. I want it to sort the array of nested json objects in "command" in alphabetical order based on the jobName value. 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);

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.

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