paint-brush
[undefined, null, NaN].sort();by@philnash
2,974 reads
2,974 reads

[undefined, null, NaN].sort();

by Phil Nash4mSeptember 3rd, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

JavaScript has a basic sorting function available on Array objects and running it will probably do what you expect. The default JavaScript sorting algorithm is to convert all members of an array to strings and then compare their sequences of UTF-16 code unit values. This works great for arrays of several numbers, but it breaks down very quickly for some numbers. The biggest problem is when the keys may be ignored and sorted to the end of the array. There are a few ways around this, but the important thing is knowing that it can happen.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - [undefined, null, NaN].sort();
Phil Nash HackerNoon profile picture
Phil Nash

Phil Nash

@philnash

Developer evangelist for Twilio and Authy. I love writing JavaScript and Ruby. console.log('🍻');

About @philnash
LEARN MORE ABOUT @PHILNASH'S
EXPERTISE AND PLACE ON THE INTERNET.
L O A D I N G
. . . comments & more!

About Author

Phil Nash HackerNoon profile picture
Phil Nash@philnash
Developer evangelist for Twilio and Authy. I love writing JavaScript and Ruby. console.log('🍻');

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Also published here