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":



  • 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.