Hello, I'm sure this question has been asked in one form or another over the years, but the answer still eludes me.

I recently wanted to sort an array, with the sort order being sorted words beginning with a lowercase letter, followed by sorted words beginning with an uppercase letter. The following code does what I want, but was wondering if there is a "niftier" way?

#!/usr/bin/perl use strict; use warnings; my @needs_sorting = qw(Psdgs ldfgs Esdds aSdg Sgsdfs esdf sgfsdfg Osgr + T); print @needs_sorting,"\n"; my @lc = sort (grep{substr($_,0,1) eq lc(substr($_,0,1)) }@needs_sorti +ng); my @uc = sort (grep{substr($_,0,1) eq uc(substr($_,0,1)) }@needs_sorti +ng); print "@lc\n"; print "@uc\n"; push @lc,@uc; print join ',', @lc,"\n";
Then I got started thinking on how to arbtrarily define the order by another array. I found the follwing snippet, but it only works for "single letters, not words. It seems it should be easy enough to extend it to words, but the solution eludes me, and I've resorted to using my first method above.
#!/usr/bin/perl use strict; use warnings; my @sorted = ("a" .. "z", "A" .. "Z"); my @needs_sorting = ( qw (P l E a S e s O r T) ); my %S; @S{@needs_sorting} = (); my @is_sorted = grep { exists $S{$_} } @sorted; print join "|",@is_sorted;
So how would I modify the above code to say sort words by the first letter, if the criteria was complex like:
my @sorted = ('a' .. 'm','n' .. 'z','X' .. 'Z','A' .. 'W');
As in:
#!/usr/bin/perl use strict; use warnings; #not working my @sorted = ('a' .. 'm','n' .. 'z','X' .. 'Z','A' .. 'W'); my @needs_sorting = ( qw (Psdf lPik Easd aKwe SSdf eqwer scfgh Oegb rq +wer T) ); my %S; @S{@needs_sorting} = (); my @is_sorted = grep { ?????? $S{$_} } @sorted; print join "|",@is_sorted;

In reply to criteria based array sorting by zentara

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.