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

Re: Argument isn't numeric in sort -- resolve or ignore?

by davido (Cardinal)
on Feb 26, 2016 at 00:17 UTC ( [id://1156157]=note: print w/replies, xml ) Need Help??


in reply to Argument isn't numeric in sort -- resolve or ignore?

Forget about those two lines for a moment. Why would your comparator return $a, or $b, or -1, 0, 1? The first and final returns will return the result of N <=> M, whereas all the other returns return the value being compared instead. That's got to be a bug.


Dave

Replies are listed 'Best First'.
Re^2: Argument isn't numeric in sort -- resolve or ignore?
by stevieb (Canon) on Feb 26, 2016 at 00:35 UTC
    A bug where, sir? In perl, or in the OPs code? I'm legitimately interested in any details/feedback you can provide.

      In the OP's code. The return $a lines will return values like 'etc'. Sort expects -1, 0 or 1 as is returned by cmp and <=>.

      Premature optimization is the root of all job security

      GrandFather did a better job of explaining it than I did.

      sort expects the comparator subroutine to return a value less than zero, equal to zero, or greater than zero for the conditions of left < right, left == right, and left > right. So the use of cmp and <=> is commonplace. But some of the possible return values of the OP's comparator are strings, which will probably not be of much use to sort. It seems as though the OP thinks he should return the value that has higher precedence in the comparison, which is a fallacy, and except for unusual circumstances, indicative of a bug in the code.


      Dave

        Yeah, it's been probably more than five years since I had to write my own sort routine and faulty memory did me in. It doesn't help that all but three of the sites in my actual data have numbers in them and most are all numbers, so it appeared to be working. Thanks for the help!!

        Elda Taluta; Sarks Sark; Ark Arks
        My deviantART gallery

Log In?
Username:
Password:

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

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

    No recent polls found