my %h = (
Foo => 73,
Bar => 66,
Rat => -1
);
If you say sort keys %h you know will get the order Bar, Foo, Rat, which means when you look through the hash in that order you will find 66, 73, -1. You could also say sort { $a cmp $b } keys %h, but that would be a little redundant.
In essence, the sort routine is going to compare Foo with Bar, Bar with Rat and Rat with Foo to decide on the order.
If, however, instead of saying {$a cmp $b} we were to say {$h{$a} <=> $h{$b}}, we would be sorting on the value that Foo et al., point to. So we would be comparing 66 with 73, 66 with -1 and 73 with -1. We have to use the spaceship operator <=> because we are performing numerical comparisons.
If we were to foreach over the hash with the results of this sort, we would see Rat, Bar, Foo.
I hope that helps you understand how it works. It is a very useful trick to have around.
--g r i n d e r
|