On Jul 3, 4:44 am, Ken Snyder <[EMAIL PROTECTED]> wrote:
> I'm trying to sort an object by its property names. As I understand it,
> iterating over an object using for...in or using Hash.each() (or
> Hash.sortBy()) does not guarantee the order of the properties.
>
> Is there an easy and low-overhead way to guarantee order?
No. The ECMAScript Language Specification says:
8.6 The Object Type
"An Object is an unordered collection of properties. Each property
consists of a name, a value and a set of attributes."
12.6.4 For-in statement
"The mechanics of enumerating the properties (step 5 in the first
algorithm, step 6 in the second) is implementation dependent. The
order of enumeration is defined by the object."
In other words, you can't depend on the properties of an object being
returned in any particular order for different browsers. You might
like to get the names (say using for..in) and push them in an array,
use Array.prototype.sort() to sort them, then use that order to get
the property values.
If order is important, perhaps the values should be stored in an
array.
--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---