[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Sort challenge



On Wed, Feb 21, 2001 at 02:45:02PM +0100, Lou Hevly wrote:
> 1) If there's a last name, the program should sort first by that and then by the firstname (if there is one);
> 2) If no last name, then sort by first name;
> 3) If neither last nor first name, then sort by username
> 
> So the desired result is:
> 
> est
> Evans
> Evans, Anne
> Evans, Helen
> Evans, Louise
> Peter
> rwj
> Sanchez

I think the followign does what you want.

#!/usr/bin/perl
chomp( @u = <DATA> );
print join "\n", map { s/\|/, /g; s/^.*?[, ]+(\w)/$1/; s/[, ]+$//; $_ }
         sort
         {
            my @a = split '\|', lc $a;
            my @b = split '\|', lc $b;
            ( $a[1] and $a[1] eq $b[1] ) ?
                ( $a[2] cmp $b[2] ) :
                ( $a[1] || $a[0] ) cmp ( $b[1] || $b[0] );
         } @u;

__DATA__
ae|Evans|Anne
le|Evans|Louise
he|Evans|Helen
pete||Peter
xs|Sanchez|
rwj||
est||
__END__


Joy,
Yanick

-- 
$,=' '; $\="\n"; @_= qw/a a a a/; @s= qw/17049 482466755 285075 95568295/;
while( (sort @s)[-1] ){ map $s[$_]-->0&&++$_[$_], (0..$#_); print @_; } 
                    ### May take some time. :) ###

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe