JS Built-ins
← Array194 / 226
ArrayArray.prototype.toSortedbeginner

Sort Strings Lexicographically

πŸ“– Usage Example
const words = ['cherry', 'apple', 'banana']
words.toSorted()  // β†’ ['apple', 'banana', 'cherry']

Call toSorted() without arguments to sort strings alphabetically into a new array.

Array.prototype.toSorted β€” Strings

Calling toSorted() without a comparator defaults to lexicographic (alphabetical) ordering β€” the same default as sort.

Implement sortStrings which returns a lexicographically sorted copy of the input string array.

πŸ’‘ Hints (2)
  • β€’ No comparator needed β€” toSorted() defaults to lexicographic order
  • β€’ Lexicographic means character-by-character comparison, like a dictionary