From: "Jan E." Date: 2012-10-20T16:01:31+09:00 Subject: Re: Hash with Integer key issue Wayne Simmerson wrote in post #1080492: > # And here is the problem! Now that I am looking at it here, it's > obvious that the sort is flattening the Hash... Is there a way to > restore it to a Hash, or a better way to achieve the sort? Hi, the standard "sort" method always returns an array of the objects coming from the "each" method. It does *not* return the original data structure. In other words: Whatever you sort, you always get back a sorted array. This usually works fine, because most of the time you just want to have the elements in a specific order. If you actually need to retain the original data structure, you have to rebuild the object from the array (as Harry and Arlen demonstrated). In this case, however, I think it's in fact a misunderstanding of hashes. The idea of a hash is to map keys to values. It doesn't have an inner order(*). So you might want to rethink if this is the right data structure for your purpose. Maybe you just want an array? (*) In newer versions of Ruby, hashes actually do have an inner order (probably to be less surprising to newbies). So they're really a hybrid of a hash an an array. However, I still wouldn't use them as an ordered sequence of elements but to map keys to values. -- Posted via http://www.ruby-forum.com/.