First of all, a Hash is not sorted, so the result cannot be what you
typed. You can sort a Hash and store the result in somethins that is
sorted, like an array. Next, you can use the method sort_by, which
sorts an Enumerable object by the computation of the block you pass to
it. Your structure is a bit complex, too much nesting there, but
something like this could work:
Like Jesus already said, you cannot sort a Hash. But of course you can
build a new Hash based on the sorted key. A Hash actually *is* ordered
by the order of which you inserted the values (I think this was
introduced in Ruby 1.9).
Apart from that I agree with Jesus that the structure is much too
complex. It's almost unreadable, a pain to process and very error-prone
(you rely on a certain structure which isn't enforced at all). You
should definitely switch to an object structure with *meaningful*
entities.
On Jul 2, 2012, at 00:52 , Jesús Gabriel y Galán wrote:
First of all, a Hash is not sorted, so the result cannot be what you
typed. You can sort a Hash and store the result in somethins that is
sorted, like an array.
Like Jesus already said, you cannot sort a Hash. But of course you can
build a new Hash based on the sorted key. A Hash actually *is* ordered
by the order of which you inserted the values (I think this was
introduced in Ruby 1.9).
Yes. I would rely on that feature for debug printing purposes only
because the order is not fixed. If you build an ordered structure
(Array) and then copy that into a Hash I think you are doing too much
work because to process items in order you can already traverse the
Array.
Apart from that I agree with Jesus that the structure is much too
complex. It's almost unreadable, a pain to process and very error-prone
(you rely on a certain structure which isn't enforced at all). You
should definitely switch to an object structure with *meaningful*
entities.
Agree as well. Classes Struct and OpenStruct go a long way in making
this much more readable. Also the usage of more telling names than
"hn2" will greatly help.
Kind regards
robert
···
On Mon, Jul 2, 2012 at 2:24 PM, Jan E. <lists@ruby-forum.com> wrote: