Sort an array of strings according to string lengths | Function to Sort the array of string according to lengths . This function implements Insertion Sort . ; Insert s [ j ] at its correct position ; Function to print the sorted array of string ; Function to perform sorting ; Calling the function to print result
function sort ( s , n ) { for ( let i = 1 ; i < n ; i ++ ) { let temp = s [ i ] ; let j = i - 1 ; while ( j >= 0 && temp . length < s [ j ] . length ) { s [ j + 1 ] = s [ j ] ; j -- ; } s [ j + 1 ] = temp ; } } function printArraystring ( str , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( str [ i ] + " " ) ; } let arr = [ " " , " " , " " , " " ] ; let n = arr . length ; sort ( arr , n ) ; printArraystring ( arr , n ) ;