array sort in alphabetical order code example

Example 1: javascript sort by numerical value

// Sort an array of numbers based on numerical value.
let numbers = [23, 65, 88, 12, 45, 99, 2000]

let sortednumbers = numbers.sort((a, b) => a - b);
//=> [12, 23, 45, 65, 88, 99, 2000]

Example 2: javascript sort alphabetically

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

// items is ['adieu', 'café', 'communiqué', 'éclair', 'premier', 'réservé']