1var items = ['réservé', 'premier', 'communiqué', 'café', 'adieu', 'éclair'];
2items.sort(function (a, b) {
3 return a.localeCompare(b); //using String.prototype.localCompare()
4});
5
6// items is ['adieu', 'café', 'communiqué', 'éclair', 'premier', 'réservé']
1// function that returns a string in alphabetical order
2function reorderStr(str) {
3 return [...str].sort().join('');
4}
5console.log(reorderStr("hacker")); //"acehkr"
6console.log(reorderStr("javascript"));//"aacijprstv"
1var names = ["Peter", "Emma", "Jack", "Mia"];
2var sorted = names.sort(); // ["Emma", "Jack", "Mia", "Peter"]
1if (item1.attr < item2.attr)
2 return -1;
3if ( item1.attr > item2.attr)
4 return 1;
5return 0;