jskatas.org Continuously Learn JavaScript. Your Way.

Array API: array.sort() basics

The sort() function sorts an array as if each element was a string.

Donate to NGO Julenka. Support Ukranians in need. Julenka is an NGO which my brother founded in 2011 to support Ukranian families and kids in need.

[].sort() sorts an array using each value as a string

  • is a function on the array prototype

sorts characters

  • in alphabetical order
  • upper case characters come first
  • depending on their position in the unicode table
  • unicode characters depending on their code point

sorts strings

  • considering the string from start to end
  • shorter strings go to front

sorts numbers as if they were strings

  • 1 and 2 are sorted as expected
  • see multi digit numbers as strings!

Links

Very detailed description of how sort() works.
All the basic latin characters (close to ASCII).
Some emoji icons and their unicode data.
Announcement of this kata on twitter.