Not sure if your computer's architecture is radically different to mine or something, but for me, this technique is still slightly slower than the functional approach, and much slower than keysort.

use v5.14; use strict; use warnings; use List::MoreUtils qw( part ); use Sort::Key qw( keysort ); use Benchmark ':all'; package Person { use Moo; has name => (is => 'ro'); has title => (is => 'ro'); } my @employees = map { Person::->new( name => int(rand(999999)), title => int(rand(2))?'Staff':'Manager', ) } 0..200; sub obvious { my @sorted = sort { if ($a->title =~ /Manager/ and not $b->title =~ /Manager/) { -1; } elsif ($b->title =~ /Manager/ and not $a->title =~ /Manager/) +{ 1; } else { $a->name cmp $b->name; } } @employees; } sub subtle { my @sorted = sort { ($a->title !~ /Manager/) - ($b->title !~ /Manager/) or $a->name cmp $b->name; } @employees; } sub functional { my @sorted = map { sort { $a->name cmp $b->name } @$_ } part { $_->title !~ /Manager/ } @employees; } sub sortkey { my @sorted = map { keysort { $_->name } @$_ } part { $_->title !~ /Manager/ } @employees; } sub pure_schwarz { my @sorted = map $_->[1], sort { $a->[0] cmp $b->[0] } map [ $_->title =~ /Manager/ ? "A".$_->name : "B".$_->name, $ +_ ], @employees; } cmpthese(1000, { obvious => \&obvious, subtle => \&subtle, functional => \&functional, sortkey => \&sortkey, pure_schwarz => \&pure_schwarz, });

Results:

              Rate     obvious       subtle pure_schwarz  functional     sortkey
obvious      133/s          --         -28%         -60%        -65%        -80%
subtle       186/s         39%           --         -45%        -52%        -72%
pure_schwarz 337/s        153%          81%           --        -12%        -50%
functional   383/s        187%         107%          14%          --        -43%
sortkey      671/s        403%         262%          99%         75%          --
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re^4: Unusual sorting requirements; comparing three implementations. by tobyink
in thread Unusual sorting requirements; comparing three implementations. by tobyink

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.