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.

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

···

On Friday, January 3, 2003, 1:52:24 PM, TOTO wrote:

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

Hi.

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.

···

At 12:04 PM 1/3/2003 +0900, Gavin wrote:

On Friday, January 3, 2003, 1:52:24 PM, TOTO wrote:

Better:

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

Gavin

···

On Friday, January 3, 2003, 2:16:29 PM, Mark wrote:

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 ...
 }

I like:

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

Daniel

···

On Friday, January 3, 2003, 2:16:29 PM, Mark wrote:

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

Hi.

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.

···

At 12:50 PM 1/3/2003 +0900, Gavin wrote:

On Friday, January 3, 2003, 2:16:29 PM, Mark wrote:

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

Better:

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

My mistake. That should have been

          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

Hi, Guy.

···

At 11:42 PM 1/3/2003 +0900, Guy wrote:

My mistake. That should have been

      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 … :frowning: )

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}”
}

puts “— sort”
hash_a.sort.each { |item|
puts “k=#{item[0]} v=#{item[1]}”
}

$ 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.