Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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} );


In reply to Re: Multiple Sort on selected column by Tanktalus
in thread Multiple Sort on selected column by huchister

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (9)
    As of 2014-12-25 20:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (163 votes), past polls