By default uses in-built semantics for comparison. But a function can be provided.
Return 0 if a and b are equal, above 0 if a is considered higher than b or below zero if b is considered higher than a.
In the below example, the default sorting semantics are reversed:
constreverse = (a, b) => { if (a === b) return0; if (a > b) return -1; if (a < b) return1; return0; // equal } sort(data, reverse); // [ 20, 9, 5, 2 ]
Returns a sorted version of
data
using a specified algorithm. Original array is left as-isBy default uses in-built semantics for comparison. But a function can be provided. Return 0 if
a
andb
are equal, above 0 ifa
is considered higher thanb
or below zero ifb
is considered higher thana
.In the below example, the default sorting semantics are reversed: