Files
30-seconds-of-code/snippets/sortCharactersInString.md
Angelos Chalaris 67cf77655c Updated examples
2017-12-27 16:06:16 +02:00

354 B

sortCharactersInString

Alphabetically sorts the characters in a string.

Split the string using split(''), Array.sort() utilizing localeCompare(), recombine using join('').

const sortCharactersInString = str =>
  str.split('').sort((a, b) => a.localeCompare(b)).join('');
sortCharactersInString('cabbage') // 'aabbceg'