js sort strings alphabetically code example
Example 1: javascript sort chars in string
const sort = str => str.split('').sort((a, b) => a.localeCompare(b)).join('');
sort('hello world');
Example 2: alphabetical order array javascript
arr.sort(function(a, b) {
return a === b ? 0 : a < b ? -1 : 1;
});
Example 3: js order alphabetically
const ascending = data.sort((a, b) => a[field].localeCompare(b[field]))
const descending = ascending.reverse()
Example 4: javascript return string in alphabetical order
function alphabet_Soup(str) {
return str.split("").sort().join("");
}
console.log(alphabet_Soup("Python"));
console.log(alphabet_Soup("Exercises"));
function alphaOrder(str) {
return str.split("").sort().join("");
}
console.log(alphaOrder("webdev"))