1 2 3 4 5 |
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); console.log(fruits); fruits.reverse(); console.log(fruits); |
by default, when we use sort, it compares by string, which is what we don’t want.
Custom Sorting
Thus, we use custom sorting
1 2 3 4 5 6 |
var points = [40, 100, 1, 5, 25, 10]; points.sort(); // wrong console.log(points); // [ 5, 40, 25, 100, 10, 1 ] // the compare function should // return a negative, zero, or positive value |
do it like so…
1 2 3 4 5 6 7 |
var random = [2,5,46,7,4,6,45,2,55,6,47,56,75,67,4,63,45,345]; random.sort(function(a,b){ if (a<b) return -1; // return a, b if (a>b) return 1; // return b, a else return 0; }); |
you can also see the ordering of integers by subtracting them. positive means left number is larger. negative means left number is smaller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// When comparing 40 and 100, the sort() method calls // the compare function(40,100). // The function calculates 40-100, and returns -60 (a negative value). // The sort function will sort 40 as a value lower than 100. points.sort(function(a, b) { // return b-a // b (100) - a (40) = 60. sort a (40) larger than b (100) --> 100 40 // return a - b // a (40) - b(100) = -60 sort a (40) smaller than b (100) --> 40 100 // if a + b // return a+b; }); console.log(points); console.log(Math.max.apply(null, points)); console.log(Math.min.apply(null, points)); |
get maximum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function getMax(arr) { let index = 0; let length = arr.length; let max = -Infinity; while (index < length) { if (arr[index] > max) { max = arr[index]; } index++; } return `max of ${arr} is ${max}`; } console.log(getMax(points)); |
get 2nd max
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function getSecondMax(arr) { console.log(`get max for array: ${arr}`); let len = arr.length; console.log(`length of array is: ${len}`) var max = -Infinity; var secondMax = -Infinity; let i = 0; while (i < len) { if (arr[i] > max) { // we found larger. // so we get secondMax to take on the previous max secondMax = max; // assign new max max = arr[i]; } i++; } // max return `second max of ${arr} is ${secondMax}`; } console.log(getSecondMax(points)); |
get minimum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function getMin(arr) { let length = arr.length; let index = 0; let min = +Infinity; while (index < length) { if (arr[index] < min) { min = arr[index]; } index++; } return `Minimum of ${arr} is ${min}`; } console.log(getMin(points)); |
Sorting Objects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
var cars = [ {type:"Mercedes", year:2001}, {type:"Volvo", year:2016}, {type:"Audi", year:2001}, {type:"Saab", year:2001}, {type:"BMW", year:2010}]; cars.sort(function(a, b){ let x = a.type; let y = b.type; console.log(`${x}, ${y} -----> is greater? ${x>y}`); //if (x < y) {return 1;} // x sorted AFTER y //if (x > y) {return -1;} // x sorted BEFORE y if (x < y) {return -1;} // go negative...x goes before y if (x > y) {return 1;} // go positive...x goes after y return 0; // do nothing }); console.log(cars); |