in reply to Re: How do I write my own sorting routine?
in thread How do I write my own sorting routine?
Note the use of the 'spaceship' comparison operator instead of the cmp operator. The reason is that cmp compares the values based on string values, while <=> compares the values based on numerical values. So if your hash has values 1 to 10, the ordering with cmp produces 1,10,2,3,..., while ordering with <=> produces 1,2,3....,10.my @sorted = sort { $hash{$a} <=> $hash{$b} } @array;
|
|---|