1

I have some array of objects, some objects can have some array again at any level, So I want all the objects in alphabetical order.

Ex:

{
    "b": [
        {
            "pqr": [
                {
                    "z": "s",
                    "t": "t"
                }
            ],
            "c": "d",
            "q": "c"
        },
        {
            "m": "h",
            "b": "g"
        }
    ]
}

I want the output as

{
    "b": [
        {
            "c": "d",
            "pqr": [
                {
                    "t": "t",
                    "z": "s"
                }
            ],
            "q": "c"
        },
        {
            "b": "g"
            "m": "h",
        }
    ]
}

after JSON.stringify(), so basically I want the sorted order at any level, arrays can also have objects which also needs to be in sorted order, I have gone through other Stack Overflow questions, but did not find this. I was trying using the below code (from https://stackoverflow.com/a/28565621/1364965, but this code does not solve my problem), but does not seem to work, recursion code does not seem to work,

function sortObject(obj) {
   var tmpArr = [];
    var tmpKeys = [];
    if (obj instanceof Array) {
        var objLen = obj.length;
        for (var i = 0; i < objLen; i++) {
            if (obj[i] instanceof Array) {
               tmpArr[i] = sortObject(obj[i]);
            } else {
                for (key in obj[i]) {
                    tmpKeys.push(key);
                }  
                tmpKeys.sort();
            }
        }
    }

    var temp = {};
    var keys = [];
    for (var key in obj) {
        keys.push(key);  
    }
    keys.sort();
    for (var index in keys) {
        temp[keys[index]] = sortObject(obj[keys[index]]);
    }
    return temp;
}

How it can be achieved?

Community
  • 1
  • 1
u_peerless
  • 644
  • 2
  • 9
  • 23
  • 6
    Objects do not maintain order. It's basically an unordered hash map. What it means is that - you cannot sort them. – zerkms Apr 11 '15 at 04:41
  • I think the OP wants to take that object and sort the output to a string which is possible. This seems like a problem set question though. What have you tried so far rather than just searching for an answer? – Jason Cust Apr 11 '15 at 04:49
  • You can do this technically (in *string* format, not using vanilla JavaScript objects)...but it will be very hard. You'll basically have to implement Python's OrderedDict and then write your own `toString` method. I hope you're patient. :) – Shashank Apr 11 '15 at 04:53
  • @JasonCust , I have updated the code which I tried, but there is some problem with recursion it seems... – u_peerless Apr 11 '15 at 07:18
  • @zerkms They're not specified to, but the actual implementations might be stable under enough circumstances to get something working. :P – Jeremy Apr 11 '15 at 07:21
  • @JeremyBanks or one might just switch to a more suitable data structure for the task. Always better than to rely on something unreliable. – zerkms Apr 11 '15 at 07:27
  • @zerkms That's certainly true, but I would enjoy reading a hacky solution if one were possible possible. – Jeremy Apr 11 '15 at 07:30
  • @JeremyBanks actually for me chrome `41.0.2272.118 m` returns the given structure "sorted" automatically. So you don't need to do anything. Unreliable - yes, hacky - yes. Where is my cookie? :-D – zerkms Apr 11 '15 at 08:00
  • @zerkms I meant a complete solution to this question. :P The author already has some code that tries to take advantage of this, but it doesn't work. One would need to figure out why. – Jeremy Apr 11 '15 at 08:02
  • @JeremyBanks instead of debugging spaghetti I prefer to implement my own http://pastebin.com/7ZK7CBNK :-) That's how I would do that if I was serious about the question. – zerkms Apr 11 '15 at 08:03

0 Answers0