I guess it depends on whether you want the list of keys sorted by which has the highest value or the values themselves. The most common use of this for me is when I want to sort the keys or values of a hash using a hash reference, for example if I'm calculating people's final scores for a scoreboard. The values would be useless sorted if I didn't know which key they went to. Take the following example, where just to mix things up a little, I've passed a hash reference to my function:
my %hash = (foo => 24, monkey => 45, kilgore => 69, buttons => 36); PrintScores(\%hash); sub PrintScores{ my $ref = $_[0]; #$ref is now a reference to %hash my @sorted_keys = sort{ $ref->{$b} <=> $ref->{$a} } keys %{$ref}; print "TOP SCORES OF THE DAY\n"; print "=====================\n"; foreach(@sorted_keys){ print "$_ => $ref->{$_}\n"; } } -----Original Message----- From: david [mailto:dzhuo@;looksmart.net] Sent: Thursday, November 07, 2002 4:25 PM To: [EMAIL PROTECTED] Subject: Re: Hash Sorting? Kurtis wrote: > Hey Timothy, > > I couldn't get it to work.......I know you can sort on keys, so I went > out > the cheap way....I switched the values with my keys...it's no problem > because my values are unique also.... > why not just sort on the values? foreach my $value (sort {$a <=> $b} values %hash){ print "$value\n"; } david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]