Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Another way is to use a Schwartzian Transform passing the key, an indicator whether the cpu is "-" (1 - TRUE or 0 - FALSE) and the cpu value in an array ref. Sorting can then be done on the indicator first then on cpu value but the second sort term is a ternary so that comparing two items with a "-" cpu value immediately returns zero.

$ perl -Mstrict -Mwarnings -E ' my $passes = { t1 => { cpu => 73, xx => 3 }, t2 => { cpu => q{-}, xx => 7 }, t3 => { cpu => 17, xx => 0 }, t4 => { cpu => 49, xx => 6 }, t5 => { cpu => q{-}, xx => 4 }, t6 => { cpu => 11, xx => 5 }, }; say for map { $_->[ 0 ] } sort { $a->[ 1 ] <=> $b->[ 1 ] || ( $a->[ 1 ] ? 0 : $a->[ 2 ] <=> $b->[ 2 ] ) } map { [ $_, $passes->{ $_ }->{ cpu } eq q{-}, $passes->{ $_ }->{ cpu }, ] } keys %$passes;' t6 t3 t4 t1 t2 t5 $

I hope this is of interest.

Update: Ignore this, it doesn't seem to be sorting correctly :-(

Update 2: I was bitten on the bum by a silly precedence problem that should have occurred to me sooner. Replacing

sort { $a->[ 1 ] <=> $b->[ 1 ] || $a->[ 1 ] ? 0 : $a->[ 2 ] <=> $b->[ 2 ] }

with

sort { $a->[ 1 ] <=> $b->[ 1 ] || ( $a->[ 1 ] ? 0 : $a->[ 2 ] <=> $b->[ 2 ] ) }

solved the problem.

Cheers,

JohnGG


In reply to Re: Numeric Sorting on Characters by johngg
in thread Numeric Sorting on Characters by halecommarachel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found