Given is an object (json) tree, where nodes on different levels might or might not contain a "sort" attribute, like this:
...
"Thomas": {
    "id": "4",
    "sort": "11"
    },
"Anna": {
    "sort": "10",
    "id": "9"
    },
"Christa": {
    "id": "13",
    "sort": "9",
    "activity" : {
        "book": "9",
        "sort": "8",
        "read": {
            "image": "9",
            "sort": "7"
            },
        "cook": {
            "image": "9",
            "sort": "6"
            }
        },
    "Peter": {
        "fish": "9",
        "sort": "5"
        }
...
I have to sort all levels individually based on the "sort" attribute and have already created a working function to sort one individual object node (convert object to array for sorting, transform back):
var sortObject = function(inObj) {
var outObj = {}, array = [], item = {};
// transform to array for sorting
array = $.map(inObj, function(value, key) {
    if($.type(value) == "object") {
       item = { nodename: key };
       for(var i in value) {
          item[i] = value[i];
       }
    } else {
        item = { [key]: value };
    }
    return item;
});
array.sort(function(a, b) {
    return a.sort - b.sort;
});
// transform back to object 
$.each(array, function(key, value) {
    if (value.hasOwnProperty("nodename")) {
        var item = {};
        for(var i in value) {
            if (i !== "nodename") {
            item[i] = value[i];
            }
        outObj[value["nodename"]] = item;    
        }
    } else {
        for(var i in value) {
            outObj[i] = value[i];    
        }
    }
});
return outObj;
};
I cannot figure out however how to apply this sort function on the entire nested object. Any hint or help is warmly appreciated. Thanks.
 
    