Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Perl Sorting One Liner

by kcott (Abbot)
on May 07, 2013 at 11:12 UTC ( #1032460=note: print w/ replies, xml ) Need Help??


in reply to Perl Sorting One Liner

G'day Siccula,

Welcome to the monastery.

Here's an example showing various techniques used in sorting columns:

$ perl -Mstrict -Mwarnings -E ' use constant { FIRST => 0, SECOND => 1, THIRD => 2 }; my @data = qw{ A:C:1 A:C:2 A:D:1 A:D:2 B:C:1 B:C:2 B:D:1 B:D:2 }; say "Raw data:"; say for @data; say "Sorted data:"; say join ":" => @$_ for sort { $b->[FIRST] cmp $a->[FIRST] || $a->[SECOND] cmp $b->[SECOND] || $b->[THIRD] <=> $a->[THIRD] } map { [ split /:/ ] } @data; ' Raw data: A:C:1 A:C:2 A:D:1 A:D:2 B:C:1 B:C:2 B:D:1 B:D:2 Sorted data: B:C:2 B:C:1 B:D:2 B:D:1 A:C:2 A:C:1 A:D:2 A:D:1

Here, FIRST column is sorted into descending alphabetical order; where items are the same, SECOND column is sorted into ascending alphabetical order; where items are the same, THIRD column is sorted into descending numerical order.

References: sort; perlvar (for $a and $b); perlop (for cmp and <=>).

Decide whether sorting will be applied to more than one column and, if so, what order to use. For instance, to sort by last name and then, if last names are the same, sort by first name, you might use something like:

$a->[LAST_NAME] cmp $b->[LAST_NAME] || $a->[FIRST_NAME] cmp $b->[FIRST +_NAME]

Look at the type of data you're sorting and use the appropriate operator: cmp for strings and <=> for numbers.

For ascending sorts (0 -> 9, A -> Z, etc.) compare $a data to $b data (e.g. $a->[SECOND] cmp $b->[SECOND]); for descending sorts, reverse the $a and $b values (e.g. $b->[THIRD] <=> $a->[THIRD]).

I've implemented the example as a one-liner (albeit with some formatting for readability purposes). So, for a one-off, throw-away command line, a one-liner is fine; however, save the code to a script if you're ever going to: run this again; need a record of what code you used; want to refer back to it to refresh your memory on how to do this.

-- Ken


Comment on Re: Perl Sorting One Liner
Select or Download Code
Re^2: Perl Sorting One Liner
by educated_foo (Vicar) on May 07, 2013 at 14:17 UTC
    This response is not just irrelevant; it is a near-farcical example of what is wrong with Perlmonks. What does the "strict and warnings" chant buy you in a one-liner? Does defining a constant FIRST as 0 improve the code? Why give an example using delimited fields when the OP is clearly using fixed-width ones?

    You parrot some random tropes that will hopefully yield upvotes, but do nothing to answer the question.

    Just another Perler interested in Algol Programming.
      Hurry up educated_foo, a few more helpful posts like this and you'll break 700 writeups by august
        I win a toaster-oven if I do that, right? Because that's the only reason I'm here.
        Just another Perler interested in Algol Programming.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1032460]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (17)
As of 2014-10-01 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (34 votes), past polls