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.)
In reply to Re: sort a hash array
by wog
in thread sort a hash array
by fuzzysteve
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |