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)
6
  • 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. Commented Jul 1, 2019 at 14:26
  • why not take an array? at least an array of keys? Commented Jul 1, 2019 at 14:27
  • As per requirement i want to go with object only Commented Jul 1, 2019 at 14:28
  • how do you like to access the data? Commented Jul 1, 2019 at 14:31
  • JavaScript does not guarantee order of property keys. Not even in ES6. Commented Jul 1, 2019 at 14:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
can you please provide demo for using map in efficient way.
if i make "0", "1", "12", "14" something like this, can i expect guarantee order in all browsers
i have updated my post by adding special character before all keys.
-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)

1 Comment

we can get above result with this Object.keys(testData).sort() that is not helpful and i should rotate whole data

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.