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

Choosing the right sort (Or: WUDsamatter?)

by friedo (Prior)
on Apr 14, 2005 at 01:34 UTC ( [id://447633]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    @files = sort { my ($ad) = ( $a =~ /fwlog\.(\d+)\w+/ ); 
                    my ($bd) = ( $b =~ /fwlog\.(\d+)\w+/ ); 
                    $ad <=> $bd } @files;
    
  2. or download this
    foo1.tla
    foo10.tla
    ...
    foo101.tla
    foo102.tla
    etc.
    
  3. or download this
    sub naive {
        sort {
    ...
            $ma <=> $mb
        } @_;
    }
    
  4. or download this
    sub orcish {       
        my %c;
    ...
            $c{$a} <=> $c{$b};
        } @_;
    }
    
  5. or download this
    sub schwartzian {
        map { $_->[0] }
        sort { $a->[1] <=> $b->[1] }
        map { [ $_, /foo(\d+)\.tla/ ] } @_;
    }
    
  6. or download this
    sub guttros {  
        map { substr( $_, 3 ) }
        sort
        map { sprintf( "%03d", /foo(\d+)\.tla/ ) . $_ } @_;
    }
    
  7. or download this
    Benchmark: timing 5000 iterations of guttros, naive, orcish, 
    schwartzian...
    ...
      ( 0.04 usr +  0.00 sys =  0.04 CPU) @ 125000.00/s (n=5000)
     schwartzian: 26 wallclock secs 
      (25.70 usr +  0.03 sys = 25.73 CPU) @ 194.33/s (n=5000)
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://447633]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 19:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found