From: Robert Klemme Date: 2011-06-22T15:47:18+09:00 Subject: Re: How to order a hash based on its keys? On Tue, Jun 21, 2011 at 6:34 PM, Iņaki Baz Castillo wrote: > 2011/6/21 Iņaki Baz Castillo : >> 2011/6/21 Robert Klemme : >>> See Adam's and Josh's replies: a Hash is generally unordered. >>> However, what do you need this for?  If it is for debugging purposes >>> then you might as well override #inspect on a per instance basis or >>> change it in Hash (not recommended). >> >> Hi, I don't understand your comment. > > Sorry, my dog pressed "sent mail". LOL That's what our cat didn't manage yet - although he is trying hard by walking over the keyboard if I am not watchful enough. > I meant that I don't understand the point of your comments. In Ruby > 1.9 Hashes are ordered. I cannot change the order after creating them, > but if you inspect elements of a Hash you get them in the order in > which they were inserted. This is valid and useful for me (in my > case). I thought you want a different ordering (based on key). >>> If you need that for other >>> reasons then maybe a tree might be a better choice.  There is for >>> example >>> http://raa.ruby-lang.org/project/ruby-rbtree/ > > I already worker with rbtree. The only difference (or one of them) is > the way in which keys are inserted. rbtree requires all the keys being > the same class. In my tests rbtree is better for deleting elements but > worse than a Hash for inserting them. benchmark Now I am confused. What do you need? A fast data structure? An ordered data structure? What is more important? You will need to do a trade off here. What are your requirements anyway? Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/