The purpose of the compare function is to define an alternative sort order.
The compare function should return a negative, zero, or positive value, depending on the arguments:
function(a, b){return a - b}
When the sort()
function compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.
If the result is negative a
is sorted before b
.
If the result is positive b
is sorted before a
.
If the result is 0 no changes are done with the sort order of the two values.
The localeCompare()
method returns a number indicating whether a reference string comes before, or after, or is the same as the given string in sort order.