Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: Problem with a sort result

by Anonymous Monk
on Jan 27, 2016 at 15:11 UTC ( [id://1153755]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Problem with a sort result
in thread Problem with a sort result

Perl often defers to C, or underlying platform, when it comes to specifications. (Sort of a hand-wave.) This seems to be the case here, as well. Read the page on qsort and compare to sort. Here's the pertinent quote:

If SUBNAME is specified, it gives the name of a subroutine that returns an integer less than, equal to, or greater than 0, depending on how the elements of the list are to be ordered.

So the behaviour is documented, and there is a discrepency in perl. You could try to fix the perl sources to address this issue. (But be sure then to run all tests. I've no clue why it's coded that way; there might be a reason.)

The following patch applies to one location only, and there are half a dozen more. Just a demonstration.

--- perl-5.22.1/pp_sort.c.orig 2015-10-17 12:38:38.000000000 +0000 +++ perl-5.22.1/pp_sort.c 2016-01-27 15:05:18.000000000 +0000 @@ -1768,11 +1768,11 @@ static I32 S_sortcv(pTHX_ SV *const a, SV *const b) { const I32 oldsaveix = PL_savestack_ix; const I32 oldscopeix = PL_scopestack_ix; - I32 result; + IV result; SV *resultsv; PMOP * const pm = PL_curpm; OP * const sortop = PL_op; COP * const cop = PL_curcop; @@ -1801,11 +1801,11 @@ while (PL_scopestack_ix > oldscopeix) { LEAVE; } leave_scope(oldsaveix); PL_curpm = pm; - return result; + return (result > 0) - (result < 0); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1153755]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2026-04-13 05:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.