Changeset 24 in mp3-find


Ignore:
Timestamp:
04/09/06 19:36:20 (18 years ago)
Author:
peter
Message:
  • use Sort::Key natural sorting in Filesystem.pm, if Sort::Key is installed
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/MP3/Find/Filesystem.pm

    r14 r24  
    99use MP3::Info; 
    1010use Scalar::Util qw(looks_like_number); 
     11 
     12eval { 
     13    require Sort::Key; 
     14    Sort::Key->import(qw(multikeysorter)); 
     15    use Sort::Key::Natural; 
     16}; 
     17my $USE_SORT_KEY = $@ ? 0 : 1; 
    1118 
    1219use_winamp_genres(); 
     
    4653    # sort the results 
    4754    if (@$sort) { 
    48         @results = sort { 
    49             my $compare; 
    50             foreach (map { uc } @$sort) { 
    51                 # use Scalar::Util to do the right sort of comparison 
    52                 $compare = (looks_like_number($a->{$_}) && looks_like_number($b->{$_})) ? 
    53                     $a->{$_} <=> $b->{$_} : 
    54                     $a->{$_} cmp $b->{$_}; 
    55                 # we found a field they differ on 
    56                 last if $compare; 
    57             } 
    58             return $compare; 
    59         } @results; 
     55        if ($USE_SORT_KEY) { 
     56            # use Sort::Key to do a (hopefully!) faster sort 
     57            #TODO: profile this; at first glance, it doesn't actually seem to be any faster 
     58            #warn "Using Sort::Key"; 
     59            my $sorter = multikeysorter( 
     60                sub { my $info = $_; map { $info->{uc $_} } @$sort }, 
     61                map { 'natural' } @$sort 
     62            ); 
     63            @results = $sorter->(@results); 
     64        } else { 
     65            @results = sort { 
     66                my $compare; 
     67                foreach (map { uc } @$sort) { 
     68                    # use Scalar::Util to do the right sort of comparison 
     69                    $compare = (looks_like_number($a->{$_}) && looks_like_number($b->{$_})) ? 
     70                        $a->{$_} <=> $b->{$_} : 
     71                        $a->{$_} cmp $b->{$_}; 
     72                    # we found a field they differ on 
     73                    last if $compare; 
     74                } 
     75                return $compare; 
     76            } @results; 
     77        } 
    6078    } 
    6179     
Note: See TracChangeset for help on using the changeset viewer.