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 [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
-~----------~----~----~----~------~----~------~--~---

Reply via email to