Is it possible to change ordering

In this code below (JSFiddle here) :

var obj = {
  "1st":"1",
  "2nd":"2",
  "4th":"4",
  "3rd":"3",
  "5th":"5"
};
console.log(obj);
for (var i in obj) { console.log(i); };

Is it possible to change the ordering of display in the console log?

I would like to display 3rd one after 2nd in the console.log(i).

Object.keys(obj).sort().forEach(function (x) { console.log(obj[x]); });

(Note: This wont work once you get up to 11th due to how .sort sorts strings.)

It feels hacky to me, but parseInt on the key should work. sort could sort if IE support isn’t needed. It might be possible to sort by keys without the parseInt. Worth giving it a try.

2 Likes

Thanks. But I would not like to use sort function because this will sort the values. In some cases I may have a requirement that I would need to display it in the following order:

var obj = {
  "1st":"1",
   "4th":"4",
  "2nd":"2",
  "3rd":"3",
  "5th":"5"
};

So, I was wondering if it’s possible to manualy set the order?

Thanks but I would prefer to set the order manually instead of using sort function because sort will guarantee either ascending or descending order.

Properties in objects are an unordered collection. You could push your object into a Map object, which does guarantee the order of elements.

Your alternative is to manually create an array of the keys you want to access in order…
["1st","5th","4th","2nd","3rd"].forEach(function (x) { console.log(obj[x]); });

You can pass your own custom compare function to sort() to sort them how you wish

arr.sort([compareFunction])

I created manual array as shown in the JSFiddle here

var obj = {
  "1st":"1",
  "2nd":"2",
  "4th":"4",
  "3rd":"3",
  "5th":"5"
};
console.log(obj);

var patArray = new Array();


patArray.push(obj["1st"]);
patArray.push(obj["2nd"]);
patArray.push(obj["3rd"]);
patArray.push(obj["4th"]);
patArray.push(obj["5th"]);


for (var i in patArray) {
  console.log(patArray[i]);
}

So, it prints in the order I want but, say for example, in future , dynamically, one of the value is deleted from the database or added in my object(by adding it in database) then this could create problem, right?

Yes, but if you cant programmatically describe the order you want an unspecified set of keys to be output in, then you will always have that problem.

Responsive code will always need an algorithm to follow. “Alphabetical order” is .sort’s default algorithm, so it can handle any values you throw at it.

I see. Thanks for your inputs. Appreciated.

Thanks for your inputs. Appreciated.

1 Like

Actually the traversal order is well defined with ES6 though.

While indeed unfortunate, this will not affect the original object – just the (temporary) array containing the keys, so you can safely sort() it:

Object.keys(obj)
  .sort((a, b) => parseInt(a, 10) - parseInt(b, 10))
  .map(key => obj[key])
  .forEach(console.log) // "1", "2", "3", "4", "5"

console.log(obj) // { "1st": "1", "2nd": "2", "4th": "4", "3rd": "3", "5th": "5" }

Also you can always make a copy of an array before performing a destructive operation:

var myArray = [3, 1, 2]
var sorted = myArray.slice().sort((a, b) => a - b)

console.log(myArray) //  [3, 2, 1]
console.log(sorted) // [1, 2, 3]
2 Likes

Thanks very much for your inputs.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.