JavaScript arrays have a builtin sort method, which mostly does what you expect: [1, 4, 2, 3].sort(); // [1, 2, 3, 4] ["d", "a", "b", "c"].sort(); // ['a', 'b', 'c', 'd'] But it has some quirks. For examples numbers are sorting as though they were s...

Markdown supported · ⌘↵ to post
Great article, but you skipped over what I believe is the biggest quirk of Array.prototype.sort(): sorting is applied in-place on the array, ie it mutates the array! This is not immediately apparent if you were to do something like const sortedArray = unsortedArray.sort(), that you just changed the unsortedArray too. I have worked on several teams who have been burned by hard-to-find bugs caused by this behaviour, which is not easily caught by a linter. I'd advise you mention this in your article too.
Marcelo Zárate
A bit of everything & a lot of nothing developer
Wow, I really love your writing! Glad you shared those edgy cases, I hope it stays in my mind. Awesome article.