When utilizing the .sort()
method of JavaScript arrays, the array is altered directly and a reference to the array is returned.
Object.keys(obj)
generates an array with all the keys from object obj
.
It's important to note that in JavaScript, object keys are not guaranteed to be in a specific order according to standards.
This brings up an interesting conflict between the two "rules": "array.sort()
is in-place" vs "Object keys are not ordered".
So what exactly happens when we execute Object.keys(obj).sort()
?
- Do the keys of the object get sorted in place and remain sorted if the object isn't modified?
- Is a temporary unspecified copy of the array of object keys created and then sorted?
- Or does something else occur?
Is this behavior explicitly outlined in the standard or does it differ based on implementation?