0

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

1 Answer 1

3
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);
Sign up to request clarification or add additional context in comments.

1 Comment

This does compile and run and my page does call my function that uses this sorting method, but when I try it out it doesn't appear to sort right. When i call the JSONObject again it appears to be in the same order, which wasn't alphabetical to start with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.