http://www.perlmonks.org?node_id=449508

olecs has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I'm having a bit of trouble sorting hashes numerically.

I create the hash entrys like this:
my %hash = (); $hash{$recNo} {$i} {'first'} = $currS; $hash{$recNo} {$i} {'last'} = $lastS; $hash{$recNo} {$i} {'min'} = $minA; $hash{$recNo} {$i} {'max'} = $minB;
For each $recNo there can be a lot of $i - both $recNo and $i are numeric.

Everything is stored properly and I can easily access it, but I can't manage to sort $i numerically.

Here is how I (try to) do it:
my $rHash = \%hash; for my $k1 = (sort keys %$rHash) #outer key, this works { for my $k2 (sort ( {keys %{rHash->{$k1}{$a}} <=> keys %{$rHash->{$k1 +}{$b}} } keys %{$rHash->{$k1}}) ) { #display miscellaneous print $k1 $k2; } }
$k1 is in the correct order, $k2 is not (rather random it seems). If I am doing an ascii sort for $k2, like this:
for my $k2 (sort keys %{$rHash->{$k1}})
it sorts correctly by ascii value, which doesnt help me much, but atleast indicates that it should be possible to sort it :)

Also, $k1 seem to be sorted correctly (numerically), why is this? I thought it sorted on ascii values unless asked to do otherwise with the <=> operator.

If someone could shed some light on this (particularly the sorting of $i) I would be very gratefull.

rgds,
Ole C.

Replies are listed 'Best First'.
Re: sorting hashes
by ambs (Pilgrim) on Apr 20, 2005 at 07:26 UTC
    What you want seems to be...
    for my $k2 (sort {$a <=> $b } keys %{$rHash->{$k1}})
    But I'm not sure (too early in the morning :-|) You want to

    Alberto Simões

Re: sorting hashes
by blazar (Canon) on Apr 20, 2005 at 07:28 UTC
    my $rHash = \%hash; for my $k1 = (sort keys %$rHash) #outer key, this works
    Is this real code? # this should NOT "work"

    I suppose you want

    for my $k1 (sort keys %$rHash)
    Oh, and BTW why this
    my $rHash = \%hash;
    additional level of referencing? (Given that you do it only to dereference it later)
Re: sorting hashes
by Limbic~Region (Chancellor) on Apr 20, 2005 at 12:37 UTC
    olecs,
    When people post hash sorting question, almost invariably, they just want to know how to sort the hash. They are not concerned with things like changing what sort routine to use or how to keep the hash in the order you want when inserting new keys and values. This is probably for good reason as hashes aren't intended to be in sorted order.

    On the other hand, sometimes that is exactly what you want which is why I wrote Tie::Hash::Sorted.

    #!/usr/bin/perl use strict; use warnings; use Tie::Hash::Sorted; my $numerically = sub { my $hash = shift; [ sort { $a <=> $b } keys %$hash ]; }; tie my %hash, 'Tie::Hash::Sorted', 'Sort_Routine' => $numerically; %hash = map { $_ => undef } 1..25; for ( keys %hash ) { tie %{ $hash{ $_ } }, 'Tie::Hash::Sorted', 'Sort_Routine' => $nume +rically; %{ $hash{ $_ } } = map { (int rand 5000) => undef } 1..5; for my $key ( keys %{ $hash{ $_ } } ) { @{ $hash{ $_ }{ $key } }{ qw/first last min max/ } = map { int +(rand 100) + 1 } 1..4; } } # Modify %hash however you want and don't worry about it for my $o_key ( keys %hash ) { for my $i_key ( keys %{ $hash{ $o_key } } ) { print "$o_key : $i_key\n"; # do something with $hash{ $o_key }{ $i_key } if you want } }
    Remember, anytime you use a tied implementation there is going to be a performance penalty. I have taken quite a few steps to optimize this module so if it is something you are interested, be sure to read the docs on how to get the most out of it. If you have any questions, please let me know.

    Cheers - L~R

Re: sorting hashes
by johnnywang (Priest) on Apr 20, 2005 at 07:28 UTC
    not sure what you're doing in the sorting of $k2, the following seem to be what you want (not tested):
    my $rHash = \%hash; for my $k1 = (sort keys %$rHash) #outer key, this works { for my $k2 (sort {$a <=> $b} keys %{$rHash->{$k1}}) ) { #display miscellaneous print $k1 $k2; } }
      The same cmt I already gave to the OP applies: this can't be what he wants for it wouldn't even compile. Correcting the typo indeed this could be what he wants.

      Oh, and BTW:

      print $k1 $k2;
      Aren't we missing something here too?!?
        Hello again,
        the = sign was a typo that is not present in my original code - sorry about that, same with the print statement - was just to indicate that there was some output.

        The solution was indeed to use sort with just $a and $b - yep, Perl is not my first language.

        Thank you all for the exceptionally quick replies.

        rgds,
        Ole C.
Re: sorting hashes
by rev_1318 (Chaplain) on Apr 20, 2005 at 08:05 UTC
    for my $k1 = (sort keys %$rHash) #outer key, this works

    No it doesn't. First, it doesn't compile. Second, you stated that $recNo is numeric and you are not sorting numeric. This should work (untested!):

    foreach my $k1 ( sort { $a <=> $b } keys %hash ) { foreach my $k2 ( sort {$a <=> $b } keys %{$hash{$k1}} ) { #do what you want, e.g. print $hash{$k1}{$k2}{first} } }

    See perldoc sort for more information.

    Paul

Re: sorting hashes
by TedPride (Priest) on Apr 20, 2005 at 14:17 UTC
    use strict; use warnings; my ($i, $j, $k, $p1, $p2, $p3, $val, %hash); for $i (0..2) { for $j (3..5) { for $k (qw/first second third/) { $hash{$i}{$j}{$k} = "$i $j $k"; } } } $p1 = \%hash; for $i (sort {$a <=> $b} keys %$p1) { $p2 = $p1->{$i}; for $j (sort {$a <=> $b} keys %$p2) { $p3 = $p2->{$j}; for $k (sort keys %$p3) { $val = $p3->{$k}; print "\$hash{$i}{$j}{$k} = '$val';\n"; } } }