From: Robert Dober Date: 2007-03-13T19:08:17+09:00 Subject: Re: sorting a hash based on a attribute of teh objects which it On 3/13/07, senthil wrote: > HI all, > I want to sort a hash based on a attribute of the objects which it > is storing.For example i am having a bean diagnosis with weightage as a > attribute.i want to sort the hash of diagnosis based on the > weightage.how to do this . > can any one explain me .. > > thanks and regards, > senthil kumar > > -- > Posted via http://www.ruby-forum.com/. > > Hashes do not define any order by definition (in Ruby), please search the list for implementations of "ordered Maps/Dictionaries/Hashes" this is a VFAQ. If you can take the time to convert your hash to a sorted array of pairs you can something like this of course. hash.to_a.sort{ |h1, h2| h1.first.whatever <=> h2.first.whatever # for keys or h1.last.whatelse <=> h2.last.whateles # for values } HTH Robert -- You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw