From: "Iñaki Baz Castillo" Date: 2011-06-22T18:50:28+09:00 Subject: Re: How to order a hash based on its keys? 2011/6/22 Robert Klemme : >> 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? Ok. I get some data and convert it into a Hash whose key is a priority value (integer, 0 is the best priority). The value of each entry is of course the data value associated with such priority. But such hash has not been ordered, this is, probably entry 1 has priority 2 while entre 2 has priority 1 (or whatever). Then I just want to get a new hash in which entries are ordered by priority. I also want to avoid too much Ruby stuff using "hast -> array -> Array#map(...) -> new has". In this thread it has been suggested using: ordered_hash = Hash[original_hash.sort] and indeed it works. Yes, it is doing hash->array->hash, but being at C level I expect it's fast enough. Regards. -- Iñaki Baz Castillo