2

If i use integer as a key in javascript object it shorts automatically.

var obj = {"z": 100, "x": 75, "d": 116, "c": 15, "10":123, "9":12}
console.log(obj)

Output:

{9: 12, 10: 123, z: 100, x: 75, d: 116, c: 15}

It is not maintaining sequence for integer, It works for alphabetical strings.

My question is Why it is so ? and how to overcome this if i need to manage sequence?

Parth Gajjar
  • 1,090
  • 3
  • 10
  • 33
  • 2
    ECMA-262 does not require Object properties to be sorted in any particular order, therefore the order is implementation dependent and you should **never** rely on it. – RobG Mar 02 '16 at 06:48
  • thanks, i just want this answer "It is not possible" :P – Parth Gajjar Mar 02 '16 at 06:51
  • It is possible with a [*Map* object](http://www.ecma-international.org/ecma-262/6.0/#sec-map-constructor), but not a plain Object (also see [*MDN: Map*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)). – RobG Mar 02 '16 at 06:55
  • yes but if i use map than i need to use get and set – Parth Gajjar Mar 02 '16 at 08:31

5 Answers5

3

Object key ordering is not guaranteed. See Does JavaScript Guarantee Object Property Order?. Use an array of objects to guarantee ordering.

var arr = [{"z":100}, {"x":75}, {"d":116}, {"c":15}, {"10":123}, {"9":12}]
Community
  • 1
  • 1
mike-schultz
  • 2,344
  • 8
  • 13
  • Arrays don't guarantee order either, however there are methods to visit the numeric properties in sequence (from low to high or high to low). – RobG Mar 02 '16 at 06:52
1

Use an array if you want to keep the order. That is one of the way to maintain the order in javascript.

Nofi
  • 1,647
  • 11
  • 18
  • Arrays aren't ordered either, that's why you should use a *for* loop or similar to visit them in sequence and never a *for..in* loop. – RobG Mar 02 '16 at 06:50
1

The order of properties in objects are not guaranteed in JavaScript. If you would like something similar to an object that does guarantee order look into the Map Object.

var myMap = new Map();
myMap.set('z', 100);
myMap.set('x', 75);
myMap.set('d', 116);
myMap.set('c', 15);
myMap.set('10',113);
myMap.set('9', 12);

Order in maps IS guaranteed

enter image description here

Arrays also keep order, yet are not keyed by names, rather by an index.

Max
  • 2,258
  • 1
  • 17
  • 31
  • Arrays don't "keep order" either, but the numeric keys can be visited in sequence. ;-) – RobG Mar 02 '16 at 06:54
1

If you want to maintain the order use "9.0", "10.0" instead of 9 or 10.

Try with this

{"z":100,"x":75,"d":116,"c":15,"10.0":123,"9.0":12}

And it is a known issue issue 164

htoniv
  • 1,550
  • 12
  • 34
  • It might be an issue, but it's not a bug in regard to compliance with ECMA-262. ;-) – RobG Mar 02 '16 at 06:53
0

Can you try sort as a map?

I have misunderstood the question before.

Object[] obj = new Object[]{"z": 100, "x": 75, "d": 116, "c": 15, "10":123, "9":12};
for (int i = 0; i < obj.length; i++)
{
    System.out.print(obj[i].toString() + " ");
}

Like

Map<String, String> objMap = new HashMap<String, String>();
objMap.put("z", 100); .....
entriesSortedByValues(objMap);

Or You can treat it like JSON

var arr = [
{name: "z",
 value:100
 }, 
.....];

then wrote a function like

funtion sortObj(column)
{
   return function(x,y)
          {
             if(x[column] > y[column])
             {
                return 1;
             }
             elseif(x[column] < y[column]){return -1;}
             else{return 0;}
          }
}

array.sort(sortObj(value));
Chuck
  • 138
  • 1
  • 6