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.

5
  • you can not sort an object. you can only sort an array with the keys and that for all levels. Commented Jun 15, 2016 at 12:12
  • You cannot sort JavaScript objects because the property ordering is implementation defined. Commented Jun 15, 2016 at 12:13
  • There is no such thing as sorting an Object. You need arrays of objects. Commented Jun 15, 2016 at 12:13
  • In the presented function, I do sorting by first converting to array, then transform it back in the new order. Commented Jun 15, 2016 at 12:33
  • So far, this is working. But how to apply it to the whole tree? Commented Jun 15, 2016 at 12:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.