How to sort each row from a two-dimensional array in Node.js

1 Answer

0 votes
let array = [
  [3, 1, 18, 4, 0],
  [5, 1, 9, 32, 8],
  [7, 21, 6, 2, 5]
];
 
// Function to sort each row
const sortedArray = array.map(row => row.slice().sort((a, b) => a - b));
 
console.log(sortedArray);
 
 
 
/*
Run:
  
[ [ 0, 1, 3, 4, 18 ], [ 1, 5, 8, 9, 32 ], [ 2, 5, 6, 7, 21 ] ]
  
*/

 



answered Mar 16 by avibootz
...