-3

Thinking we have a JSON object. What we supposed to do is sort that JSON object by key and do not break the order of that JSON object.

for example:

var data = {
  "321":{
          number:26,
          name: 'l',
          took_date: '12/01/1993',
          left_date: '12/24/1995'
        },
  "341":{
          number:2,
          name: 'h',
          took_date: '12/23/1934',
          left_date: '04/19/1940'
        },
  "513":{
          number:7,
          name: 'i',
          took_date: '07/01/1957',
          left_date: '05/01/1960'
        },
  "123":{
          number:16,
          name: 'b',
          took_date: '03/12/1921',
          left_date: '03/12/1922'
        },
  "890":{
          number:58,
          name: 'w',
          took_date: '07/19/2012',
          left_date: '09/12/2014'
        }
}

I want to sort this JSON and do not break its order. Sorry for the confusion.

If I use

Object.keys(data).sort();

It can be sorted by id but its order will be break.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
PPXia
  • 43
  • 7
  • 4
    [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – adeneo Oct 13 '16 at 00:25
  • Do you mean you want to create a copy of the original, with the copy sorted and the original left as is? Object properties don't really have a defined order. Do you mean an array? – nnnnnn Oct 13 '16 at 00:25
  • Completely unclear. Provide an example. – PM 77-1 Oct 13 '16 at 00:25
  • 2
    `Object.keys(obj).sort()` will give you a sorted list of an objects keys – Jaromanda X Oct 13 '16 at 00:28
  • json.parse(whatever_json_you_need_to_parse); – Feathercrown Oct 13 '16 at 00:34
  • 2
    Objects can not be sorted since they do not have order.... – epascarello Oct 13 '16 at 00:34
  • Looks like someone wasn't paying enough attention in class.... – Feathercrown Oct 13 '16 at 00:35
  • You cannot sort an Object unless it's an Array. – StackSlave Oct 13 '16 at 00:38
  • In short, I think what you want to do, is make data an array and make the keys of the object just a property on each array item. That'll be easier to work with if the order of the keys displayed is the order you actually want. – Bill Criswell Oct 13 '16 at 00:45
  • Even after the edit it isn't clear what the problem is. How are you trying to use the results of `Object.keys(data).sort();`? That doesn't change the original object in any way, it produces a *new* array of just the keys which you could then iterate over to process the original object in that order. There is no way to "break" the order of your object, because *objects don't have an order*. – nnnnnn Oct 13 '16 at 00:46

2 Answers2

0

Sort the keys of the object, and then create a new object an loop through the keys and assign.

var newObj = {}, sortedKeys = Object.keys(data).sort();

sortedKeys.forEach(function(key) {
  newObj[key] = obj[key]
})
Dan
  • 774
  • 5
  • 11
  • Objects do not have order... http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – epascarello Oct 13 '16 at 00:35
  • This is essentially creating two of the same objects. You can't create an object with the keys in order. You can keep an array of the keys in order and then just go through that instead looking up the key from the original object. – Bill Criswell Oct 13 '16 at 00:37
  • @epascarello no they do not, but by creating an array of the keys, it can be sorted and then a new obj created (which was the op requirement anyway) – Dan Oct 13 '16 at 00:37
  • 1
    Why is it not guaranteed? If it needs to be sorted in a certain way they can use a compareFunction – Dan Oct 13 '16 at 00:45
  • When iterating over an object using a `for..in` loop the property order is not guaranteed. (In practice browsers generally iterate in the order the properties were created, but you shouldn't rely on that.) – nnnnnn Oct 13 '16 at 00:48
0

Here you go:

function numericObjectWalk(obj, func, context){
  var a = [], c = context || this;
  for(var i in obj){
    a.push(+i);
  }
  a.sort(function(a, b){
    return a-b;
  });
  for(var i=0,v,l=a.length; i<l; i++){
    v = a[i]; func.call(c, v, obj[v]);
  }
  return obj;
}
numericObjectWalk(yourObject, function(prop, obj){
  if(prop === 341){
    obj.name = 'new name';
  }
});
StackSlave
  • 10,613
  • 2
  • 18
  • 35