in reply to sort direction
That sort routine doesn't work too well when sorting string fields. When sorting string fields, number are considered greater than strings. In ASCII, it's the other way around.
>perl -le "print '123' <=> 'abc'" 1 >perl -le "print '123' cmp 'abc'" -1
It would be better if you knew if the field was numerical or a string in advance. The following makes all decision making in advance, speeding up the sorting.
my $orderby = 'age'; my $numerical = 1; my $direction = 'ASC'; my $sorter; if ($direction eq 'ASC') { if ($numerical) { $sorter = sub { $hash{$a}{$orderby} <=> $hash{$b}{$orderby} }; } else { $sorter = sub { $hash{$a}{$orderby} cmp $hash{$b}{$orderby} }; } } else { if ($numerical) { $sorter = sub { $hash{$b}{$orderby} <=> $hash{$a}{$orderby} }; } else { $sorter = sub { $hash{$b}{$orderby} cmp $hash{$a}{$orderby} }; } } @sorted = sort { &$sorter } keys %hash;
|
|---|