sort array of strings in javascript code example
Example 1: sorting array from highest to lowest javascript
let numbers = [5, 13, 1, 44, 32, 15, 500]
let lowestToHighest = numbers.sort((a, b) => a - b);
let highestToLowest = numbers.sort((a, b) => b-a);
Example 2: array sort by alphabetical javascript
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: array sort js
arr = ['width', 'score', done', 'neither' ]
arr.sort()
arr.sort((a,b) => a.localeCompare(b))
arr = [21, 7, 5.6, 102, 79]
arr.sort((a, b) => a - b)