Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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
[GrandFather]: It's a good day end here Corion. The start was somewhat less than average!
[Corion]: GrandFather: All's well that ends well? ;)
[GrandFather]: I'm fighting with a third party device our software is to support. The documentation for the device's SDK is quite a lot less than average and most of today was spent ...
[GrandFather]: discovering that one of the sensors for the device lies about the gain range it is using!
[GrandFather]: However, by the end of the day I had discovered its deceptions and now have it working correctly, so yes, all's well that ends well. :-D
[Corion]: GrandFather: Ah, (hardware) APIs - I have a similar situation with Chrome and its API... It is fairly underdocumented and I guess I have to hunt...
[Corion]: ... down supposedly working code to find out what I'm supposed to do

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2017-08-24 07:06 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (365 votes). Check out past polls.