javascript sort list 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: sort array of objects javascript
list.sort((a, b) => (a.color > b.color) ? 1 : -1)
Example 3: javascript sort array smallest to largest
var numArray = [140000, 104, 99];
numArray.sort(function(a, b) {
return a - b;
});
console.log(numArray);
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)
Example 5: JavaScript Sorting Arrays
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();