RE: [?] Sort array by keys...
JScript arrays can't have strings as keys, you're just extending the Object instance with W, X, Y and Z properties. They're not actually array elements, you can easily see this when you check MyArr.length (it'll still be 0).
As for sorting object properties, I see two possible solutions. You could create a new object and define the properties in the sorted order, as Eljay proposed. Or you could keep the same object and delete the properties with delete MyObj[K] and then re-add the properties. This preserves prototypes as you're working on the same object you started with.
However, like Eljay I also wonder why you'd need to do this anyway. Objects are great to retrieve a value by its key name and store values with their keys. The order of those values is not important for these tasks since objects don't and shouldn't behave like ordered lists. If you really need hashes with sorting behavior, you might be better off creating something like a custom sorted hash table class with getter/setter and enumerator methods.
|