sort an array alphabetically typescript 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: how to sort string aray in ts
const value = ['2020', '2017', '2019', '2018']
value.sort((a,b) => a < b ? -1 : a > b ? 1 : 0)
value.sort((a,b) => a < b ? 1 : a > b ? -1 : 0)
const value = ['2020', '2017', '2019', '2018']
Array.from(value).sort((a,b) => a-b)
Array.from(value).sort((a,b) => b-a)