in reply to Re: sort hash by value
in thread sort hash by value

To expand on ant9000's answer, his function will help in making a comparison tag. He's building a string which has all the numbers in fixed-width columns so that the string comparison operator will naturally order according to numerical fields. To use it to sort by those tags, you'd apply the schwartzian transform:

my @keys_sorted_by_converted_value = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, fmt($hash{$_}) ] } keys %hash;

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re: Re: Re: sort hash by value
by ant9000 (Monk) on Jul 11, 2003 at 14:55 UTC
    You're right: reading again my post, I find it quite cryptic myself... sorry.
    The idea is the following:
    @sorted_keys=sort { fmt($hash{$a}->{oid}) <=> fmt($hash{$b}->{oid}) } keys %hash;
    where fmt is defined as above (with the correction in split below, sure!). You can also use cmp instead of <=>, if you wish.