Googling around I found this function to sort an array by integer size easily, and tested it out in jsfiddle, works as intended (console logs 140000, 104, 99).
function sortNumber(a,b) {
return b - a;
}
var numArray = [104, 99, 140000];
numArray.sort(sortNumber);
console.log(numArray);
So I tried to implement it in a solution for a freeCodeCamp exericse, and somehow can't get it to work. The only difference is that now I have a nested array to process. So I want to loop trough all the arrays nested in the arr element (for example largetsOfFour), and sort the integers inside the arrays by size. Can anyone point out what I'm doing wrong?
function largestOfFour(arr) {
for(var x=0; x<arr.lenght; x++) {
arr[x] = arr[x].sort(function(a,b) { return b - a; });
}
return arr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);