It's even faster if you avoid the sort block, using GRT:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw /timethese cmpthese/; our @advocates = map {{fld_title => 'Rec' . $_, fld_type => [qw /National State Local/] -> [ra +nd 3]}} 1 .. shift || 1000; our %order = (LOCAL => 'A', STATE => 'B', NATIONAL => 'C'); our %order_r = reverse %order; our (@limbic, @GRT); cmpthese -1 => { limbic => '@limbic = sort {$order {uc $a -> {fld_type}} cmp $order {uc $b -> {fld_type}} or $a -> {fld_title} cmp $b -> {fld_titl +e}} @advocates', GRT => '@GRT = map {{fld_title => substr ($_, 1), fld_type => $order_r {substr $_, 0, 1}} +} sort map {join "", $order {uc $_ -> {fld_type}}, $_ -> {fld_title}} @a +dvocates', }; my $limbic = join " " => map {$_ -> {fld_title}} @limbic; my $GRT = join " " => map {$_ -> {fld_title}} @GRT; die "Unequal\n" unless $limbic eq $GRT; __END__ Rate limbic GRT limbic 61.9/s -- -49% GRT 121/s 95% --
It does lose the case of fld_type, but that's easily fixable.

Abigail


In reply to Re: Efficiently sorting array by non-alpha strings by Abigail-II
in thread Efficiently sorting array by non-alpha strings by knowmad

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.