Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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) { Person.new(:$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'} +)\ .sort.map(*.value.sort(*.name)); say .perl for @sorted;

Output:

Person.new(name => "Alice", title => "Sales Manager") Person.new(name => "Carol", title => "Marketing Manager") Person.new(name => "Greg", title => "Finance Manager") Person.new(name => "Bob", title => "Customer Services") Person.new(name => "Dave", title => "Janitor") Person.new(name => "Eve", title => "Tech Support") Person.new(name => "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

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (6)
    As of 2014-07-14 01:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (254 votes), past polls