1

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?

4
  • 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. Commented Mar 2, 2016 at 6:48
  • thanks, i just want this answer "It is not possible" :P Commented Mar 2, 2016 at 6:51
  • It is possible with a Map object, but not a plain Object (also see MDN: Map). Commented Mar 2, 2016 at 6:55
  • yes but if i use map than i need to use get and set Commented Mar 2, 2016 at 8:31

5 Answers 5

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}]
Sign up to request clarification or add additional context in comments.

1 Comment

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).
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

1 Comment

It might be an issue, but it's not a bug in regard to compliance with ECMA-262. ;-)
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.

1 Comment

Arrays don't "keep order" either, but the numeric keys can be visited in sequence. ;-)
1

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

1 Comment

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.
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));

4 Comments

i don't want sorting i just want to get same order like before
So, you just want to output the sequence like what is inside the array?
yes, in the same order as i initialize object. Currently it changing order
I have update the code at the top. See whether it works

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.