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// Sort an array of numbers
2let numbers = [5, 13, 1, 44, 32, 15, 500]
3
4// Lowest to highest
5let lowestToHighest = numbers.sort((a, b) => a - b);
6//Output: [1,5,13,15,32,44,500]
7
8//Highest to lowest
9let highestToLowest = numbers.sort((a, b) => b-a);
10//Output: [500,44,32,15,13,5,1]
11
1// Alphabetically
2const ascending = data.sort((a, b) => a[field].localeCompare(b[field]))
3// Descending
4const descending = ascending.reverse()
5
1users.sort(function(a, b){
2 if(a.firstname < b.firstname) { return -1; }
3 if(a.firstname > b.firstname) { return 1; }
4 return 0;
5})
6