in reply to Re: Multiple Sort on selected column
in thread Multiple Sort on selected column
I agree with one small caveat. It might be better to change the cmp_by sub to do a numeric comparison in addition to the string comparison. With just the cmp, 600000 sorts after 55000 but 400000 would sort before. The following will sort numerically largest to smallest then alphabetically.
sub cmp_by { my $result; for my $term (@_) { $result ||= +$b->{$term} <=> +$a->{$term} or $a->{$term} cmp $ +b->{$term}; } return $result; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Multiple Sort on selected column
by kennethk (Abbot) on Oct 19, 2012 at 15:07 UTC |