Sort 2D array in JavaScript -
i'm trying sort numbers in each array greatest smallest, first 1 being sorted. need nested loop? i'm stuck.
function sortnums(arr) { for(var = 0; < arr.length; i++){ arr[i] = arr[i].sort(function(a, b){return b-a;}); return arr; } } sortnums([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
when using return
keyword function terminated. move outside iteration , should good:
function sortnums(arr) { for(var = 0; < arr.length; i++) { arr[i] = arr[i].sort(function(a, b) { return b - a; }); } return arr; } sortnums([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Comments
Post a Comment