2

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.

Malvolio
  • 256
  • 4
  • 15

1 Answers1

2

Hi I am asnwering this for the future refernces because the question is promising. You can use Underscore.js instead and make it easy.

 var arrayTreeSort = function (array, parent, tree) {
  try {
    var iValue = $scope.iValue + 1;
    tree = typeof tree !== 'undefined' ? tree : [];
    parent = typeof parent !== 'undefined' ? parent : {
      ROLE_SEQ: 0
    };
    var children = _.filter(array, function (child) {
      return child.ROLE_PARENT_SEQ == parent.ROLE_SEQ;
    });
    if (!_.isEmpty(children)) {
      if (parent.ROLE_SEQ == 0) {
        tree = children;
      } else {
        parent['sort'] = children
      }
      _.each(children, function (child) {
        var arrayTreeSort(array, child)
      });
    }
    return tree;
  } catch (err) {
    exceptionService.promiseRejectsAfterAWhile(err);
  }
};

var sortedArray = arrayTreeSort(unsortedArray);

Give your input array as parameter of the arrayTreeSort() function. It will return the sorted array to the variable sortedArray.

Insidet he function, I statically given sort as parameter. Here the sort with value 0 will be act as parent and other will come uner this.

I hope this will help. Thank you.