Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Choosing the right sort (Or: WUDsamatter?)

by friedo (Prior)
on Apr 14, 2005 at 01:34 UTC ( #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
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? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2015-07-04 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (59 votes), past polls