in reply to hashes sorted by value, but displaying both keys and values

How about this?

for my $person (sort { $values{$a} <=> $values{$b} } keys %values) { print "$person\t$values{$person}", br(); }

Update: Removed incorrect dereferencing arrows and changed sorting scheme. I'd originally done this as a Schwartzian Transform before realizing that it was unnecessary. Less post, more sleep.

Replies are listed 'Best First'.
Re: Re: hashes sorted by value, but displaying both keys and values
by Anonymous Monk on Dec 31, 2003 at 22:30 UTC
    Hi thanks for that, but I also need to sort alphabetically if the same number is associated with a number of people, such as ->
    Steve 1000
    John 977
    Alice 555
    Mike 555
    Zoe 555

    I'm currently getting something like ->
    Steve 1000
    John 977
    Mike 555
    Alice 555
    Zoe 555

      No problem, just throw an extra comparison in the sort block as documented in perldoc -f sort:

      sort { $values{$a} <=> $values{$b} || $a cmp $b } keys %values