I have already covered basic sorting behavior of Array.prototype.sort
function in JS in a blog post.
Sort function is not straightforward due to some internal interpretation on default sort function. Also, its default behavior on any type is to convert to string and then compare.
On the same lines, null
and undefined
are treated differently.
In default implementation (no custom sort function)
undefined
is handled as a primitive as they are ignored and sorted to the end.null
to the string “null” which will appear somewhere in the middle of the alphabet.