Row wise sorting in 2D array | JavaScript Program to sort 2D matrix row - wise ; loop for rows of matrix ; loop for column of matrix ; loop for comparison and swapping ; swapping of elements ; printing the sorted matrix ; Driver code
function sortRowWise ( m ) { for ( let i = 0 ; i < m . length ; i ++ ) { for ( let j = 0 ; j < m [ i ] . length ; j ++ ) { for ( let k = 0 ; k < m [ i ] . length - j - 1 ; k ++ ) { if ( m [ i ] [ k ] > m [ i ] [ k + 1 ] ) { let t = m [ i ] [ k ] ; m [ i ] [ k ] = m [ i ] [ k + 1 ] ; m [ i ] [ k + 1 ] = t ; } } } } for ( let i = 0 ; i < m . length ; i ++ ) { for ( let j = 0 ; j < m [ i ] . length ; j ++ ) document . write ( m [ i ] [ j ] + " " ) ; document . write ( " " ) ; } return 0 ; } let m = [ [ 9 , 8 , 7 , 1 ] , [ 7 , 3 , 0 , 2 ] , [ 9 , 5 , 3 , 2 ] , [ 6 , 3 , 1 , 2 ] ] ; sortRowWise ( m ) ;