Imagine you were given a string and you had to sort that string alphabetically using a function. Example:
sortAlphabets( \'drpoklj\' ); //=> returns \'djklopr
Newer browsers support String.prototype.localeCompare() which makes sorting utf8
encoded strings really simple. Note that different languages may have a different order of characters. More information on MDN about localCompare.
function sortAlphabet(str) {
return [...str].sort((a, b) => a.localeCompare(b)).join("");
}
console.log(sortAlphabet("drpoklj")); // Logs: "djklopr"
If you only have to support ascii strings then the default sorting implementation will do.
function sortAlphabet(str) {
return [...str].sort().join("");
}