sort the hash keys 
Author Message
 sort the hash keys

How can I sort the keys of a Hash so that they can be outputed
according to the keys sorted order?

Thanks.



Tue, 21 Jun 2005 10:49:05 GMT  
 sort the hash keys

Quote:

> How can I sort the keys of a Hash so that they can be outputed
> according to the keys sorted order?

A Hash is explicitly a non-sorted data structure.  If you just want
the keys in sorted order, use:

  hash.keys.sort

Look at other methods in Hash (e.g. sort) for more info.

Gavin



Tue, 21 Jun 2005 11:04:31 GMT  
 sort the hash keys

Hi.


Quote:

> > How can I sort the keys of a Hash so that they can be outputed
> > according to the keys sorted order?

>A Hash is explicitly a non-sorted data structure.  If you just want
>the keys in sorted order, use:

>   hash.keys.sort

>Look at other methods in Hash (e.g. sort) for more info.

You can also use hash.sort directly, however, be careful.  It returns
an array of [key, value].  So to sort and iterate over the hash hash_a
do

     hash_a.each { |item|
       key = item[0]
       value = item[1]
       # do stuff ...
     }

Regards,

-mark.



Tue, 21 Jun 2005 11:16:29 GMT  
 sort the hash keys

Quote:

> You can also use hash.sort directly, however, be careful.  It returns
> an array of [key, value].  So to sort and iterate over the hash hash_a
> do
>      hash_a.each { |item|
>        key = item[0]
>        value = item[1]
>        # do stuff ...
>      }

Better:

  hash_a.each do |key, value|
    # do stuff
  end

Gavin



Tue, 21 Jun 2005 11:50:08 GMT  
 sort the hash keys

Quote:

>> You can also use hash.sort directly, however, be careful.  It returns
>> an array of [key, value].  So to sort and iterate over the hash hash_a
>> do
> Better:

>   hash_a.each do |key, value|
>     # do stuff
>   end

I like:

hash.keys.sort.each do |key|
    # do stuff with ~key~ and ~hash[key]~
end

Daniel



Tue, 21 Jun 2005 12:24:20 GMT  
 sort the hash keys

Hi.


Quote:

> > You can also use hash.sort directly, however, be careful.  It returns
> > an array of [key, value].  So to sort and iterate over the hash hash_a
> > do

> >      hash_a.each { |item|

My mistake.  That should have been

          hash_a.sort { |item|  # item is array [key, value]

Sorry about the confusion.

-mark.

Quote:
> >        key = item[0]
> >        value = item[1]
> >        # do stuff ...
> >      }

>Better:

>   hash_a.each do |key, value|
>     # do stuff
>   end



Tue, 21 Jun 2005 22:20:44 GMT  
 sort the hash keys

M> My mistake.  That should have been

M>           hash_a.sort { |item|  # item is array [key, value]

 I don't understand, sorry, but Hash#sort will use the block to sort the
 hash and item is [[key0, value0], [key1, value1]]

 no ?

 and you can write it

   hash_a.sort do |(k0, v0), (k1, v1)|
      # compare k0, v0, k1, v1
   end

Guy Decoux



Tue, 21 Jun 2005 22:42:50 GMT  
 sort the hash keys

Hi, Guy.


Quote:

>M> My mistake.  That should have been

>M>           hash_a.sort { |item|  # item is array [key, value]

>  I don't understand, sorry, but Hash#sort will use the block to sort the
>  hash and item is [[key0, value0], [key1, value1]]

(I have to stop sprouting when I am sick .. :-( )

What I -meant- to say was "hash_a.sort.each".  So,

$ cat hash_a.rb
#! /usr/local/bin/ruby
#
hash_a = Hash.new
hash_a["bob"] = 1
hash_a["al"]  = 2
hash_a["zac"] = 3
hash_a["moe"] = 4

puts "--- each"
hash_a.each { |key, val|
     puts "k=#{key} v=#{val}"

Quote:
}

puts "--- sort"
hash_a.sort.each { |item|
     puts "k=#{item[0]} v=#{item[1]}"

Quote:
}

$ ruby hash_a.rb
--- each
k=al v=2
k=moe v=4
k=zac v=3
k=bob v=1
--- sort
k=al v=2
k=bob v=1
k=moe v=4
k=zac v=3

--
My apologies for the wasted bandwidth.

-mark.



Wed, 22 Jun 2005 01:57:55 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Accessing hash values sorted by their keys

2. Sorting a Hash by value of integer stored in the Hash

3. hash as key in hash

4. Hash.new(Hash.new) doesn't use Hash.new as default value

5. GOBO - Sorting Hash tables

6. Sorting a Hash

7. Array = Hash.sort ... but sometimes not

8. Sort by hash element

9. Sorting an array of hashes

10. FW: Sorting an array of hashes

11. ++operator and sorted hashes

12. Sorting a Hash

 

 
Powered by phpBB® Forum Software