1

I have object contain data like this

const testData = {
        "11": {
            "data": {
                "firstName": "firstName",
                "lastName": "lastName"
            }
        },
        "12": {
            "data": {
                "firstName": "firstName",
                "lastName": "lastName"
            }
        },
        "00": {
            "data": {
                "firstName": "firstName",
                "lastName": "lastName"
            }
        },
        "01": {
            "data": {
                "firstName": "firstName",
                "lastName": "lastName"
            }
        },
        "final": {
            "data": {
                "firstName": "firstName",
                "lastName": "lastName"
            }
        }
    }

i want to sort this data by object keys like 00, 01, 11, 12, final

i have tried like this but i can not achieve what i want.Any idea would be appreciate?

sorted = Object.keys(testData).sort().reduce((acc, key) => ({
  ...acc, [key]: testData[key]
  }), {})

console.log(sorted)
Madara's Ghost
  • 158,961
  • 49
  • 244
  • 292
U rock
  • 677
  • 2
  • 11
  • 27
  • values who could be indices of arrays are sorted first, then by insertation order. double zero is not an index, as well as values with leading zeros. – Nina Scholz Jul 01 '19 at 14:26
  • why not take an array? at least an array of keys? – Nina Scholz Jul 01 '19 at 14:27
  • As per requirement i want to go with object only – U rock Jul 01 '19 at 14:28
  • how do you like to access the data? – Nina Scholz Jul 01 '19 at 14:31
  • [JavaScript does not guarantee order of property keys](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order). [Not even in ES6](https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties). – VLAZ Jul 01 '19 at 14:38
  • But browser displays content in correct order with out sorting – U rock Jul 01 '19 at 14:39

2 Answers2

7

You cannot. The defined iteration order for JavaScript object keys is as follows:

  1. Numeric keys, in ascending numeric order. (this includes strings such as "11", but not "01"), THEN...
  2. String keys which are not numeric, in order of insertion. THEN...
  3. Symbol keys, in order of their insertion.

As you can see, regardless of insertion order, numeric keys will always appear first in the iteration order, and always in their numeric order.

In your example, "11" and "12" will always end up first, regardless of what you do.

In general, relying on order with objects (which are essentially unordered dictionaries you access via key), is ill-advised. If order matters, you should be using an array. Alternatively, you can use a Map.

Madara's Ghost
  • 158,961
  • 49
  • 244
  • 292
  • what if i add 0 infront of "11" like this "011". this will sort as we want, after sorting we need to remove first 0 in all keys. – U rock Jul 01 '19 at 20:23
  • The moment you have, in your object, the key "11" and the key "01", "11" will always come first no matter what. If you add a padding 0, and then remove it before you put it in the object, you will accomplish nothing. Seriously though, use a Map, or an array. – Madara's Ghost Jul 01 '19 at 20:31
  • can you please provide demo for using map in efficient way. – U rock Jul 01 '19 at 20:38
  • if i make "0", "1", "12", "14" something like this, can i expect guarantee order in all browsers – U rock Jul 02 '19 at 07:02
  • i have updated my post by adding special character before all keys. – U rock Jul 02 '19 at 08:15
-2

Try something like this:

sorted = Object.keys(testData).sort((a,b) => {
    if (a < b) return -1;
    if (a > b) return 1;
    return 0;
});

console.log(sorted)
  • we can get above result with this `Object.keys(testData).sort()` that is not helpful and i should rotate whole data – U rock Jul 01 '19 at 14:35