Example 1: sort javascript array
var points = [40, 100, 1, 5, 25, 10];
points.sort((a,b) => a-b)
Example 2: sort js
numArray.sort((a, b) => a - b);
numArray.sort((a, b) => b - a);
Example 3: how the sort function works javascript
const unsorted = ['d', 'd', 'h', 'r', 'v', 'z', 'f', 'c', 'g'];
const sorted = unsorted.sort();
console.log(sorted);
const unsortedNums = [45, 56, 3, 3, 4, 6, 7, 45, 1];
const sortedNums = unsortedNums.sort((a, b) => {
return a - b;
});
console.log(sortedNums);
Example 4: javascript sort function
var points = [40, 100, 1, 5, 25, 10];
points.sort((a,b) => a-b)
Example 5: sort js array array
var ages = [18, 21, 9, 41, 35, 24]
ages.sort(function(a, b) {
return a - b
})
Example 6: sort function explained javascript
var sortedArray = myArray.sort(function(a,b){
if (a.name < b.name)
return -1;
else if (a.name == b.name)
return 0;
else
return 1;
});