javascript array string sort alphabetically code example
Example 1: javascript sort alphabetically
var items = ['réservé', 'premier', 'communiqué', 'café', 'adieu', 'éclair'];
items.sort(function (a, b) {
return a.localeCompare(b);
});
Example 2: javascript string array sort alphabetically
var items = ['réservé', 'premier', 'communiqué', 'café', 'adieu', 'éclair'];
items.sort((a, b) =>
a.localeCompare(b)
);
Example 3: sort a string in javascript
var string='ACBacb';
var sortedString = string.split('').sort().join('');
Example 4: put array in alphabetical order
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();