Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

sorta sorting fun

by softworkz (Monk)
on Feb 07, 2008 at 20:16 UTC ( #666850=perlquestion: print w/ replies, xml ) Need Help??
softworkz has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks. While having a typical day fumbling around in MS access sorting some records. A co-worker comes in and asks whadia sorting by yada yada later on he says... Your lucking "they" didn't ask for you to sort by Middle initial, 2nd letter of their first name and last letter of last name.

Knowing it can be done with Perl I'd like to slap this list on his desk and say here, there you go! lol Any ideas would be helpful looking at sort, reverse and cmp but stuck on part 2nd letter of first name..

Here's an example, thanks!

INPUT
FNAME LNAME MNAME SURNAME
john,doe,sam,jr
albert,simpson
barry,white,,III
harry,potter
steve,zurk,james,sr


OUTPUT
barry,white,,III
harry,potter
albert,simpson
steve,zurk,james,sr
john,doe,sam,jr

Comment on sorta sorting fun
Re: sorta sorting fun
by moritz (Cardinal) on Feb 07, 2008 at 20:27 UTC
    Sorting by second letter of first name:
    my @sorted = sort { substr($a, 1, 1) cmp substr($b, 1, 2) } @names;

    The others aren't much more complicated, you just have to enhance the comparison in the curly braces. Or, even better, do a Schwartzian Transform.

Re: sorta sorting fun
by kyle (Abbot) on Feb 07, 2008 at 21:33 UTC
    use strict; use warnings; use Data::Dumper; my @in = qw( john,doe,sam,jr albert,simpson barry,white,,III harry,potter steve,zurk,james,sr ); my @in_split = map { [ split /,/ ] } @in; my @in_hashed = map { { fname => $_->[0], lname => $_->[1], mname => $_->[2], surname => $_->[3], } } @in_split; my @in_sorted = sort { ( defined $a->{mname} && defined $b->{mname} && ( lc substr( $a->{mname}, 0, 1 ) cmp lc substr( $b->{mname}, + 0, 1 ) ) ) || ( lc substr( $a->{fname}, 1, 1 ) cmp lc substr( $b->{fname}, 1, +1 ) ) || ( lc substr( $a->{lname}, -1, 1 ) cmp lc substr( $b->{lname}, -1 +, 1 ) ) } @in_hashed; print Dumper \@in_sorted; __END__ $VAR1 = [ { 'lname' => 'white', 'mname' => '', 'fname' => 'barry', 'surname' => 'III' }, { 'lname' => 'potter', 'mname' => undef, 'fname' => 'harry', 'surname' => undef }, { 'lname' => 'simpson', 'mname' => undef, 'fname' => 'albert', 'surname' => undef }, { 'lname' => 'zurk', 'mname' => 'james', 'fname' => 'steve', 'surname' => 'sr' }, { 'lname' => 'doe', 'mname' => 'sam', 'fname' => 'john', 'surname' => 'jr' } ];

    Formatting is by perltidy.

    Since some records don't have a middle name, you have to check if that's defined to avoid warnings. In that case, I've skipped comparing on middle name. If you want undef names to sort in a particular place (first or last, I'd guess), then you could code for that.

Re: sorta sorting fun
by roboticus (Canon) on Feb 08, 2008 at 03:56 UTC
    That simply shows that they don't know SQL. There's no particular trick to it.
    select FName, LName, MName, Surname from TABLE order by substring(MName,1,1), substring(FName,2,1), substring(LName,datalength(LName),1)
    This is with MS SQL. It should work on other SQL servers, though the functions may be different...

    ...roboticus

      ++Excellent help!! Thanks!! ++ for the example using MySQL

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://666850]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-12-19 06:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (72 votes), past polls