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,
undefined are treated differently.
In default implementation (no custom sort function)
undefinedis handled as a primitive as they are ignored and sorted to the end.
- Sorting will convert
nullto the string “null” which will appear somewhere in the middle of the alphabet.