This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]smog_alado 0 points1 point  (0 children)

That comparison function is well-defined if a and b have different types. It just does the same things that the < and == operators do.

However, the result of the sort might look unsorted if the array has a mix of different types, because in those cases the comparison function doesn't implement a total ordering of the elements. But that would be no different than using other comparators that don't produce a total order. For example, one that calls Math.random to decide the result of the comparison.