Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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!



Replies are listed 'Best First'.
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 (Chancellor) 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...


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://666850]
Approved by moritz
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 2016-06-30 08:51 GMT
Find Nodes?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...

    Results (391 votes). Check out past polls.