Here's a quick little subroutine that I wrote awhile back that I use a fair amount. It will sort a multidimensional hash by the value of whatever "column" you specify and throw the sorted keys back to you.

BTW, I sometimes use this more than once in a script to filter results down...that is why I pass an array ref as the last arg (so I can filter if need be).

Was able to remove the temp hash based on what Juerd wrote...Could have sworn I tried formatting the compare that way.

$hash{'1'}->{"person"} = "Me"; $hash{'1'}->{"age"} = "27"; $hash{'2'}->{"person"} = "Wife"; $hash{'2'}->{"age"} = "26"; $hash{'3'}->{"person"} = "Dad"; $hash{'3'}->{"age"} = "54"; $hash{'4'}->{"person"} = "Brother"; $hash{'4'}->{"age"} = "24"; my @sortedKeys = sortHashByColumn(\%hash,"age","desc",[keys %hash]); foreach my $sortedKey(@sortedKeys){ print "$sortedKey: " . $hash{$sortedKey}->{'age'} . " " . $hash{$s +ortedKey}->{'person'} . "\n"; } sub sortHashByColumn{ my($hashRef,$sortColumn,$sortDirection,$keysAR) = @_; my @sortedKeys; my $sortType; # VERIFY THAT THIS IS A VALID COLUMN if(! defined $hashRef->{$$keysAR[0]}->{$sortColumn}){ print "sortHashByColumn Error: $sortColumn is not a column in +this hash.\n"; exit(0); # VALID COLUMN, FIGURE OUT IF IT IS A NUMERIC COLUMN OR ALPHA } else { if($hashRef->{$$keysAR[0]}->{$sortColumn} =~ /^\d+/){ $sortType = "numeric"; } else { $sortType = "alpha"; } } if($sortType eq "numeric"){ if($sortDirection =~ /asc/i){ @sortedKeys = sort { $hashRef->{$a}{$sortColumn} <=> $hash +Ref->{$b}{$sortColumn} } @$keysAR; } else { @sortedKeys = sort { $hashRef->{$b}{$sortColumn} <=> $hash +Ref->{$a}{$sortColumn} } @$keysAR; } } else { if($sortDirection =~ /asc/i){ @sortedKeys = sort { $hashRef->{$a}{$sortColumn} cmp $hash +Ref->{$b}{$sortColumn} } @$keysAR; } else { @sortedKeys = sort { $hashRef->{$b}{$sortColumn} cmp $hash +Ref->{$a}{$sortColumn} } @$keysAR; } } return @sortedKeys; }

In reply to Sort Multidimensional Hash By Column by mdog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.