From: Daniel Carrera Date: 2003-03-18T08:59:34+09:00 Subject: Re: Accessing hash values sorted by their keys On Tue, Mar 18, 2003 at 08:49:45AM +0900, Josef 'Jupp' Schugt wrote: > Is an elegant way of doing this? > > h.keys.sort.each { |key| > puts h[key] > } > > works but seems unneccarily complicated. I don't think that this looks complicated, but here is an alternative: h.sort will return an array of arrays. Like this: >> h = { "name" => "Daniel", "field" => "Math" } => {"name"=>"Daniel", "field"=>"Math"} >> h.sort => [["field", "Math"], ["name", "Daniel"]] So Hash#sort will sort by the keys and return a 2-dimmensional array. So your snipet could be written as: h.sort.each { |item| puts item[0] } I don't know if you think that's any simpler. But if all you really want is to print the key values, just do: puts h.keys.sort But I suspect that this is not what you really want to do and your snipet was just an example. -- Daniel Carrera Graduate Teaching Assistant. Math Dept. University of Maryland. (301) 405-5137