in reply to Re^2: Argument isn't numeric in sort -- resolve or ignore?
in thread Argument isn't numeric in sort -- resolve or ignore?

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

Replies are listed 'Best First'.
Re^4: Argument isn't numeric in sort -- resolve or ignore?
by Argel (Prior) on Feb 26, 2016 at 12:05 UTC
    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

      In the end, your intuition was correct (don't ignore the warnings unless you know why they're happening), and the warnings served their purpose (alerted you to a potential bug). They may not have explicitly come out and said what was wrong with the code, but they did point to the region of code where the Perl runtime was sensing a possible problem, and with a little digging you presumably have been able to fix the issue..


      Dave

        Right you are. I do not like warnings I do not understand, so after reading up on smartmatch (which I thought was the problem) and searching on the error message, I decided to post here to see if someone could figure out the problem. And I took the advice and fixed the return codes for my_sort and replaced smartmatch with greps, and have a working custom sort routine now. I also didn't know smartmatch was moved into experimental status, which was an added bonus. Thanks again to everyone!!

        Elda Taluta; Sarks Sark; Ark Arks
        My deviantART gallery