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.


In reply to Re: sorta sorting fun by kyle
in thread sorta sorting fun by softworkz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.