Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

You can exploit that fact that sort is stable, and do two passes over the data:

sub two_pass { my @sorted = sort { $b->title =~ /Manager/ <=> $a->title =~ /Manager/ } sort { $a->name cmp $b->name } @employees; }

Moo wouldn't install on my machine, so I used Mo instead for benchmarking:

Rate obvious subtle two_pass functional obvious 173/s -- -22% -30% -61% subtle 222/s 28% -- -11% -50% two_pass 249/s 44% 12% -- -44% functional 444/s 156% 100% 79% --

Of course it does more than necessary (splitting into managers and non-managers first is less work, because the lists to sort are shorter), but the speed still isn't too bad.

Update: and here's a Perl 6 solution:

use v6; class Person { has $.name; has $.title; } sub person($name, $title) {$name, :$title) }; my @employees = person("Eve", "Tech Support"), person("Alice", "Sales Manager"), person("Dave", "Janitor"), person("Fred", "Receptionist"), person("Bob", "Customer Services"), person("Greg", "Finance Manager"), person("Carol", "Marketing Manager"), ; my @sorted = @employees.classify({ .title ~~ /Manager/ ?? 'manager' !! 'staff'} +)\*.value.sort(*.name)); say .perl for @sorted;

Output: => "Alice", title => "Sales Manager") => "Carol", title => "Marketing Manager") => "Greg", title => "Finance Manager") => "Bob", title => "Customer Services") => "Dave", title => "Janitor") => "Eve", title => "Tech Support") => "Fred", title => "Receptionist")

.classify returns a Hash, and .sort on a Hash returns a list of Pair object, sorted by key and then by value. Since 'manager' lt 'staff', this puts all the managers first. Then each of the lists is sorted by name.

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

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (5)
    As of 2018-06-24 11:00 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.