DEV Community

Discussion on: Haskell Quicksort in JavaScript

Collapse
 
sethcalebweeks profile image
Caleb Weeks

Good catch! Without explicitly comparing against undefined, my version would fail for arrays containing zeros. Since this is a sorting algorithm, I'll assume that an array of numbers is being passed in. I'll update to correct it!

Collapse
 
amn3s1a2018 profile image
Amn3s1a2018 • Edited

Sorry, i just replaced your original bug with a bigger one. And maybe i didn't emphasized enough, that those zeros are not just the zeros. Boolean false, empty string and even the document.all (in browser it's an array or object with type of "undefined" – what?) is falsy...

Thread Thread
 
sethcalebweeks profile image
Caleb Weeks

Yeah, that's true. But I guess what should you expect if you try to put a boolean, empty string, or an object in a number sorting algorithm... There is a reason why Array.sort coerces all items in the array to strings.