Forum: Ruby Hash sort

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Shandy N. (Guest)
on 2009-03-20 19:36
I am trying to sort a hash and it seams to be working, the issue is that
the sort goes from Z-A. Is there a way to make it sort A-Z? My code is
pretty straight foward, I have a hash - @users - and I just say
@hash.sort to sort it.

Thanks,

-S
Shandy N. (Guest)
on 2009-03-20 22:50
Shandy N. wrote:
> I am trying to sort a hash and it seams to be working, the issue is that
> the sort goes from Z-A. Is there a way to make it sort A-Z? My code is
> pretty straight foward, I have a hash - @users - and I just say
> @hash.sort to sort it.
>
> Thanks,
>
> -S

Figured it out. Part of my issue was that the key - which are string -
that I was trying to sort were in upper and lowercase, once I converted
them to lower case everything was fine. Here is the code I used:

@users.sort.each { |f| puts "#{f[0]} : #{f[1]}" }

This will print out the key (in f[0]) and the value (in f[1]).
This topic is locked and can not be replied to.