The return value of the compareFunction described in MDN can be less than 0 or greater than 0, it does not necessary have to be exactly -1 or 1.
This is consistent with the behavior of the comparing functions of many other programming languages, and allows returning simply the difference a - b when the comparing elements are numbers.
The return value of the
compareFunction
described in MDN can be less than 0 or greater than 0, it does not necessary have to be exactly -1 or 1.This is consistent with the behavior of the comparing functions of many other programming languages, and allows returning simply the difference
a - b
when the comparing elements are numbers.See also this discussion on stackoverflow.