[].sort()
sorts an array using each value as a string
is a function on the array prototype
GIVEN characters to be sorted
WHEN sorting them THEN they are sorted in alphabetical order
WHEN the values contain upper case characters THEN these come first
WHEN sorting THEN the order depends on the character's position in the unicode table
WHEN sorting unicode characters THEN they are sorted depending on their code point
GIVEN strings are sorted
considering the string from start to end
shorter strings go to front
Numbers are sorted as if they were strings!
WHEN sorting [2
, 1
] THEN this is sorted as expected and returns [1
, 2
]
WHEN sorting multi digit numbers THEN they are "seen" as string!
What is returned and what is sorted?
WHEN sort()
is called on an array literal THEN the sorted result is the return value
WHEN a variable (that is an array) is sorted THEN the actual array's content is modified
WHEN a variable is sorted THEN the variable and the returned value references the same object/array
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.
Related Katas
Array API
array.sort()
basicsarray.sort()
can take a compare functionarray.shift()
(as introduced in ES3)array.push()
(as introduced in ES3)Array.from()
Array.of()
array.fill()
array.find()
array.findIndex()
array.entries()
array.keys()
array.values()
array.includes()
array.toReversed()
Difficulty Level
BEGINNER
First Published
22 October 2015
Stats
12 tests to solve