How to sort each row from a two-dimensional array in TypeScript

1 Answer

0 votes
let array: number[][] = [
    [3, 1, 4, 0],
    [5, 1, 9, 8],
    [7, 6, 2, 5]
];

// Function to sort each row
const sortedArray: number[][]  = array.map(row => row.slice().sort((a, b) => a - b));

console.log(sortedArray);



/*
Run:
 
[[0, 1, 3, 4], [1, 5, 8, 9], [2, 5, 6, 7]]
 
*/

 



answered Mar 16, 2025 by avibootz
...