array sort by string code example

Example 1: javascript string array sort alphabetically

var items = ['réservé', 'premier', 'communiqué', 'café', 'adieu', 'éclair'];
items.sort((a, b) =>
   a.localeCompare(b)//using String.prototype.localCompare()
);

Example 2: javascript order by string array

users.sort((a, b) => a.firstname.localeCompare(b.firstname))

Example 3: sort a string in javascript

var string='ACBacb';
var sortedString = string.split('').sort().join('');

Example 4: sort function in react js

this.state.data.sort((a, b) => a.timeM > b.timeM ? 1:-1).map(
    (item, i) => <div key={i}> {item.matchID} {item.timeM} {item.description}</div>
)