Files
30-seconds-of-code/snippets/sortCharactersInString.md
Angelos Chalaris 97c250bcfb Updated examples
2017-12-27 16:35:25 +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'