There is a small issue in your fix - it would return -1 if both of them are null.
But I actually wanted to bring up a more interesting point - I felt that the original implementation was not necessarily a bug, if it was written for some specific business logic. It may have been intentional to treat null as equal to any value. As you pointed out, it effectively keeps nulls where they are. That may very well have been the original requirement.
And it's still consistent, despite how it goes against our instinct that null ought to be less than any non-null value. Conversely, it also could be consistent and sensible, in some other contexts, to consider null greater than any non-null value.
Well, only whoever was familiar with the original circumstances would be able to answer those questions. :-) It could very well have been a bug. I just would have given the original author some benefit of doubt, and assumed that they did what they did for a reason until proven otherwise.
I definitely agree that if this was intended, some comment should have been put in place.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
There is a small issue in your fix - it would return
-1
if both of them arenull
.But I actually wanted to bring up a more interesting point - I felt that the original implementation was not necessarily a bug, if it was written for some specific business logic. It may have been intentional to treat
null
as equal to any value. As you pointed out, it effectively keepsnull
s where they are. That may very well have been the original requirement.And it's still consistent, despite how it goes against our instinct that
null
ought to be less than any non-null value. Conversely, it also could be consistent and sensible, in some other contexts, to considernull
greater than any non-null value.Thanks for your comment.
I agree, that might have been a requirement. However, this brings two questions:
null
values in their original location?EDITED: I updated the code accordingly
Well, only whoever was familiar with the original circumstances would be able to answer those questions. :-) It could very well have been a bug. I just would have given the original author some benefit of doubt, and assumed that they did what they did for a reason until proven otherwise.
I definitely agree that if this was intended, some comment should have been put in place.