Files
30-seconds-of-code/snippets/sortCharactersInString.md
2017-12-28 08:30:19 +00:00

18 lines
370 B
Markdown

### sortCharactersInString
Alphabetically sorts the characters in a string.
Split the string using `split('')`, `Array.sort()` utilizing `localeCompare()`, recombine using `join('')`.
```js
const sortCharactersInString = str =>
str
.split('')
.sort((a, b) => a.localeCompare(b))
.join('');
```
```js
sortCharactersInString('cabbage'); // 'aabbceg'
```