Organizing a Hash Map

Hello,
I have a hash map that gets pumped full of info. The key is a
username… I’m using a @myhashmap.each do |key, item| to display the
hash map in my rhtml, but I need it to be alphabetized… Is there a
built-in way to do this?

Thanks,

  • Jeff M.

On 12 May 2008, at 21:46, Jeff M. wrote:

Hello,
I have a hash map that gets pumped full of info. The key is a
username… I’m using a @myhashmap.each do |key, item| to display the
hash map in my rhtml, but I need it to be alphabetized… Is there a
built-in way to do this?

hashes in ruby don’t have a guaranteed order (at least not in 1.8.
Ruby 1.9 is different). Your best bet is probably to sort the keys and
iterate on that.

Fred

On Mon, May 12, 2008 at 10:46:46PM +0200, Jeff M. wrote:

Hello,
I have a hash map that gets pumped full of info. The key is a
username… I’m using a @myhashmap.each do |key, item| to display the
hash map in my rhtml, but I need it to be alphabetized… Is there a
built-in way to do this?

@myhashmap.sort.each do |key, item| …

Thanks,

  • Jeff M.
    –Greg

sort worked great, thanks!

On 5/12/08, Jeff M. [email protected] wrote:

Hello,
I have a hash map that gets pumped full of info. The key is a
username… I’m using a @myhashmap.each do |key, item| to display the
hash map in my rhtml, but I need it to be alphabetized… Is there a
built-in way to do this?

gem install orderedhash


Greg D.
http://destiney.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs