Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Perl Sorting One Liner

by kcott (Chancellor)
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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1032460]
[erix]: one would hope evolutionists haven't any prayers anyway
[marto]: obviously someone could be religious, but not creationist
[erix]: "Nothing in Intelligent Design makes sense except in the light of Creationism" <-- I made that one up myself (free after Dobzhansky )
[erix]: yes. Deplorable marto, deplorable.
[marto]: the situation seemed similar to this one, majority of the contributrions are nonsense, doesn't address any questions ...
[marto]: meh, I've been called worse :P
[marto]: deplorable is actually not bad for this point in the day :P

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2017-07-28 15:47 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (431 votes). Check out past polls.