[Home] > Snippets  > Languages  > JavaScript  > Strings  >  Sort the characters of a string in the alphabetical order

Sort the characters of a string in the alphabetical order

JavaScript

const sort = (str) => str
.split('')
.sort((a, b) => a.localeCompare(b))
.join('')

Examples

sort('hello world') // dehllloorw