http://www.perlmonks.org?node_id=999803


in reply to Multiple Sort on selected column

Why not Sort::Key? Is it just because it's on CPAN? (Yes, even you can use CPAN) If it's just for learning, that's one thing, but if that were the case, why use sort instead of rolling your own sorting routine? Ok, I have the answer for that: you don't want to learn the details of sorting, you just want to learn how to use sort. But to that I say that it's an arbitrary line, and, for productivity reasons, I'd suggest learning Sort::Key and Sort::Key::Multi/Sort::Key::Maker as your arbitrary line instead. So, instead of the idiomatic:

@ranked = sort { $a->{FIRST} cmp $b->{FIRST} or $a->{LAST} cmp $b->{LA +ST} or $a->{AGE} <=> $b->{AGE} } @employees;
(note that we're comparing ages with the <=> operator, not cmp, so that 9 and 19 will sort properly, using cmp will not cause these to sort properly) learning to use Sort::Key might be more useful. For example:
#!/usr/bin/perl use strict; use warnings; use Sort::Key qw(multikeysorter); my @employees = ( { FIRST => 'Bill', LAST => 'Gates', SALARY => 600000, AGE => 45 }, { FIRST => 'George', LAST => 'Tester', SALARY => 55000, AGE => 29 }, { FIRST => 'Sally', LAST => 'Developer', SALARY => 55000, AGE => 29 }, { FIRST => 'Joe', LAST => 'Tester', SALARY => 55000, AGE => 29 }, { FIRST => 'Steve', LAST => 'Ballmer', SALARY => 600000, AGE => 41 } ); my %types = ( FIRST => 'str', LAST => 'str', SALARY => 'int', AGE => 'int', ); my @sort = @ARGV; # not comma-separated, just for ease @sort = map uc, @sort; # so I don't have to hold the shift key down. my $sorter = multikeysorter( sub { my $A = $_; map { $A->{$_} } @sort +}, @types{@sort} ); my @ranked = $sorter->(@employees); foreach my $emp (@ranked) { print "$emp->{SALARY}\t$emp->{AGE}\t$emp->{FIRST}\t$emp->{LAST}\n" +; }
And then, as an example:
$ perl x.pl first age 600000 45 Bill Gates 55000 29 George Tester 55000 29 Joe Tester 55000 29 Sally Developer 600000 41 Steve Ballmer $ perl x.pl age first 55000 29 George Tester 55000 29 Joe Tester 55000 29 Sally Developer 600000 41 Steve Ballmer 600000 45 Bill Gates $ perl x.pl age last first 55000 29 Sally Developer 55000 29 George Tester 55000 29 Joe Tester 600000 41 Steve Ballmer 600000 45 Bill Gates
Simple, huh? :-)

Update: And for reversing, add the following line right after the my %types declaration:

@types{map -$_, keys %types} = map -$_, values %types;
This will create the key -FIRST with value -str. And then the my $sorter line becomes:
my $sorter = multikeysorter( sub { my $A = $_; map { $A->{s/^-//r} } @ +sort }, @types{@sort} );
This removes the - sign when looking up the value in the employee hash, but keeps the - sign when passing in the types to sort. And now?
$ perl5.14.2 x.pl -age last first 600000 45 Bill Gates 600000 41 Steve Ballmer 55000 29 Sally Developer 55000 29 George Tester 55000 29 Joe Tester
Note that the /r flag is new in 5.14.0, if you are running something older, you'll have to use:
my $sorter = multikeysorter( sub { my $A = $_; map { (my $k = $_) =~ s +/^-//; $A->{$k} } @sort }, @types{@sort} );

Replies are listed 'Best First'.
Re^2: Multiple Sort on selected column
by huchister (Acolyte) on Oct 19, 2012 at 14:05 UTC
    Wasn't exactly the answer I was looking for, but you shown me the alternative way to solve this which may come in handy in near future. thanks!