in reply to Re: problem in sorting has by value
in thread problem in sorting hash by value

foreach my $key (map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_, $thishash->{$_} ] } keys %$thishash;
If the sort field is just a hash key lookup, its not worthwhile to do the Schwartzian thing (even with the hash dereference). You have to build an array reference, and then dereference it, so its only worth it for more expensive operations. Just do a straight sort:<code> sort { $thishash->{$b} <=> $thishash->{$a} } keys %thishash;

Replies are listed 'Best First'.
Re: Re: Re: problem in sorting has by value
by dragonchild (Archbishop) on Dec 11, 2001 at 22:00 UTC
    I agree that doing a straight sort in this case is more efficient, but if the user wanted to use pre-defined sorting function, this is what would have to be done.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.