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

RFC:A brief tutorial on Perl's native sorting facilities.

by BrowserUk (Pope)
on Feb 05, 2007 at 19:55 UTC ( #598394=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
     print for sort qw[ 12 a123 a122 A123 B123 Ab123 aB123 456 1A23 1a23 ]
    +;;
    12          ## ord('1') == 49, ord('2') == 50
    ...
    a122
    a123
    aB123
    
  2. or download this
    print for sort{ $b cmp $a } qw[ 1 10 100 2 20 21 3 300 ];;
    300
    ...
    100
    10
    1
    
  3. or download this
    print for sort{ $b <=> $a } qw[ 1 10 100 2 20 21 3 300 ];
    300
    ...
    3
    2
    1
    
  4. or download this
    print for sort{ 
        substr( $a, 1 ) <=> substr( $b, 1 ) 
    ...
    D222
    A473
    B659
    
  5. or download this
    ## Build an array of anonymous arrays, 
    ## each of which contains the sort field and the original element.
    ...
    print Dumper \@sorted;;
    
    $VAR1 = ['E001','C123','D222','A473','B659'];
    
  6. or download this
    @sorted = map{ 
        $_->[1] 
    ...
    
    print Dumper \@sorted;;
    $VAR1 = ['E001','C123','D222','A473','B659'];
    
  7. or download this
    print for map{ 
        $_->[2] 
    ...
    A473
    B659
    C659
    
  8. or download this
    print for map{ 
        ## Chop off the bit we added.
    ...
    D222
    A473
    B659
    
  9. or download this
    print for map{ 
        unpack 'x[N] A*', $_ 
    ...
    D222
    A473
    B659
    
  10. or download this
    print for map{ 
        unpack 'x[NA1]A*', $_ 
    ...
    A473
    B659
    C659
    
  11. or download this
    #! Perl -slw
    use strict;
    ...
    Jan 1997
    Jan 1998
    Dec 1998
    
  12. or download this
    C:\test>junk
    Jan 1997
    ...
    FY05
    Apr 2006
    Dec 2007
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://598394]
Approved by liverpole
help
Chatterbox?
[marto]: Wolfsbane , now I'm having flashbacks
[choroba]: Isn't Using PerlPod Creatively rather a meditation?
[choroba]: I don't see a question
[1nickt]: ugh, I stuck my head in the bass bin for 30 seconds on a dare at Ted Nugent at Hammersmith Odeon. Yes, I am 40% deaf now.
[johngg]: My daughter is incredibly jealous of my wife who got to see The Clash at Brixton many years ago. They went to see The Vaccines there recently.
[1nickt]: But the bands are even louder! I saw Spearhead (Michael Franti) at an outdoor show and had to walk a mile away to not feel pain in my chest! Babies were crying ... I asked the sound engineer why it was necessary to have the bass so loud and he laughed...
[Discipulus]: but the best i attended live was Mano Negra Patchanka at Forte Prenestino .. in 1990
[Corion]: Hmmm - Mano Negra or at least Manu Chao seem to put on a good live show. At least the one live CD I have from Manu Chao sounds good ;)
Discipulus feels the same jealousity of the johngg's daughter
[1nickt]: choroba I agree

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2017-03-24 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (301 votes). Check out past polls.