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? Or would I need to have some type of collection object that uses arrays instead of the property-value configuration that Hash uses? Here is an example object similar to the one I want to sort: var foodsByLetter = { ''B'': [''broccoli'', ''bread''], ''C'': [''carrots'', ''celery''], ''A'': [''alfalfa sprouts'', ''apple''] }; Then I want to sort by the object''s keys so I can create a nested list: <ul> <li>A<ul><li>alfalfa sprouts</li><li>apple</li></ul></li> <li>B<ul><li>broccoli</li><li>bread</li></ul></li> <li>C<ul><li>carrots</li><li>celery</li></ul></li> </ul> So for example, I could do $H(foodsByLetter).keys().sort(); to sort the keys alphabetically but then I wouldn''t know which letter related to which food list. Any help is appreciated. -- Ken Snyder --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Jul 3, 4:44 am, Ken Snyder <kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
RobG wrote:> 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. > > ...Very helpful. I think that is what I was missing. Thanks, Ken var foodsByLetter = { ''B'': [''broccoli'', ''bread''], ''C'': [''carrots'', ''celery''], ''A'': [''alfalfa sprouts'', ''apple''] }; Instead of: for( var letter in foodsByLetter ) { // out of order foodsByLetter[letter]; } I can do: Object.keys(foodsByLetter).sort().each(function(letter) { foodsByLetter[letter]; }); --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Apparently Analagous Threads
- Multiple language output - Correct in RGui, wrong in .txt after sink()
- mixed-effects model with two fixed effects: interaction
- Tree connect failed: ERRDOS - ERRnomem
- Setting base level for contrasts with lme
- RFC: making the xen startup integrate better with distros