Accoring to the documentation for sort, it expects a function that returns -1, 0, or 1 for (a<b),(a==b),(a>b), respectively. So, that's all your sub needs to do.
# note passing your sub $a and $b foreach my $id (sort { mysort($direction,$a,$b) } keys %hash) { print "$id - $hash{$id}{$orderby}\n"; } # end-foreach sub mysort { my ($direction, $_a, $_b) = @_; my $result = $hash{$_a}{$orderby} <=> $hash{$_b}{$orderby} || $hash{$_a}{$orderby} cmp $hash{$_b}{$orderby}; # default to ascending, but if the dir is 'DESC', we # return the *opposite* answer return ($direction eq 'DESC') ? -($result) : $result; }
Untested, of course, but the idea should be basically sound. If I understand what you're trying to accomplish, though, you might take a different approach.
# we pass the sub the actual values to compare foreach my $id ( sort { mysort($direction,$hash{$a}{$orderby},$hash{$b}{$orderby}) } +keys %hash ) { print "$id - $hash{$id}{$orderby}\n"; } # end-foreach sub mysort { my ($direction, $_a, $_b) = @_; # generalized, simplified comparison my $result = $_a <=> $_b || $_a cmp $_b; # default to ascending, but if the dir is 'DESC', we # return the *opposite* answer return ($direction eq 'DESC') ? -($result) : $result; }
In reply to Re: sort direction
by radiantmatrix
in thread sort direction
by rsiedl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |