method sort not sorting last nested array JavaScript -


somehow arr[i].sort() not sorting last nested array , getting bad result. tryed , while, different operators, nothing helped. im doing wrong? have return biggest numbers in array.

function largestoffour(arr) {    var = 0;    while (i != arr.length) {      arr[i] = arr[i].sort().pop();      i++;    }    return arr;  }    largestoffour([    [4, 5, 8, 1, 3],    [13, 27, 18, 26],    [32, 35, 37, 39],    [1000, 1001, 817, 1]  ]);

the problem in sort defaults - sorts array strings - "8..." > "1..."

try following:

function cmp(a,b){   return a-b; }  function largestoffour(arr) {   var = 0;   while (i != arr.length) {     arr[i] = arr[i].sort(cmp).pop();     i++;   }   return arr; }  largestoffour([   [4, 5, 8, 1, 3],   [13, 27, 18, 26],   [32, 35, 37, 39],   [1000, 1001, 817, 1] ]); 

output: [ 8, 27, 39, 1001 ]


Comments