loading...

Discussion on: A sorting bug

Collapse
jingxue profile image
Jing Xue

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.

Collapse
nfrankel profile image
Nicolas Frankel Author

Thanks for your comment.

I agree, that might have been a requirement. However, this brings two questions:

  1. Why do no comment this strange behavior?
  2. Why would you sort everything, but keep null values in their original location?

EDITED: I updated the code accordingly

Collapse
jingxue profile image
Jing Xue

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.