Am 30.11.2012 07:22, schrieb sto.mar / web.de:
> Am 30.11.2012 07:15, schrieb sto.mar / web.de:
>> Am 30.11.2012 02:46, schrieb Doug Jolley:
>>> I am running Ruby v. 1.8.7 and I have installed the orderedhash gem.  I
>>> know that the purpose of orderedhash is to preserve the order of
>>> creation.  However, what I'd really like to do is, after the hash has
>>> been created, sort it on the key in a manner similar to the way that I
>>> would sort an array with the sort!() method and then preserve the sorted
>>> order.  Any ideas on how I might accomplish that feat?  Thanks for any
>>> input.
>>>
>>>       ... doug

You can access the elements in sorted order even more easily like this:

   hash = {:c => 1, :a => 2, :b => 3}

   hash.sort_by(&:to_s).each do |k, v|
     puts "#{k} => #{v}"
   end

output:

a => 2
b => 3
c => 1


-- 
<https://github.com/stomar/>