PerlTextSort

ALSO SEE: PerlGrepExactMatch

Here's a way to sort a list of textual entries, avoiding sorting on things like "the" and "a". It's not the most efficient code in the world (comments welcome!), but it works. I use it to sort album and artist names when building the lists in CooferCatMusic.

sub text_sort

 {<br />
        my $x=$a;<br />
        my $y=$b;5

$x=~s/^(the|les|le|a)\s+//gi;

        $x=~tr/A-Z/a-z/;7

$y=~s/^(the|les|le|a)\s+//gi;

        $y=~tr/A-Z/a-z/;9

$x cmp $y;

 }11

MAIN:

 {<br />
        my @list=('The Killers', 'Kenicke', 'Konstruction');13

@list=sort text_sort @list;

 }15

Similarly, if you want to do a genuine numeric sort (and not the usual alphanumeric sort), this might do the job for you (again, not necessarily the most efficient, but it works):

sub numeric_sort

 {<br />
        my $x=$a;<br />
        my $y=$b;19

$x=~s/\D+.*$//g;

        $y=~s/\D+.*$//g;<br />
        $x=$x+0;<br />
        $y=$y+0;21

$x <=> $y;

 }23

There are better ways to do both of these things, but they might be a good start if you're looking for this sort of thing. By all means tell us your super-smart ways of doing the same thing ;-)