in reply to sort a hash array
my @array1 = sort { $queryterms{$b} <=> $queryterm{$a} || $a cmp $b } keys %queryterms; my @array2 = map { $queryterms{$_} } @array1;
Smaller. Faster. Alternately one could just do my @sortedkeys = sort { $queryterms{$b} <=> $queryterm{$a} || $a cmp $b } keys %queryterms; and then use @queryterm{@sortkeys} instead of @array1 and @sortedkeys (better named, IMO) instead of @array1.
update: added key comparing to sort, like the original had.
(update: I would note that if you can't use perl's built-in sort, you probably should use a better algorithm. Yours takes O(N**2) comparisions where as perl's sort algorithm (and most of the good sorting algortihms) average O(N log N) comparisions. )
(I forewarn that I have not tested the code here.)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: sort a hash array
by fuzzysteve (Beadle) on Nov 18, 2001 at 09:45 UTC |
In Section
Cool Uses for Perl