Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged | Function that returns the string s in sorted form such that the positions of alphabets and numeric digits remain unchanged ; String to character array ; Sort the array ; Count of alphabets and numbers ; Get the index from where the alphabets start ; Now replace the string with sorted string ; If the position was occupied by an alphabet then replace it with alphabet ; Else replace it with a number ; Return the sorted string ; Driver code
function sort ( s ) { var c = s . split ( ' ' ) ; c . sort ( ) ; var al_c = 0 , nu_c = 0 ; while ( c [ al_c ] . charCodeAt ( 0 ) < 97 ) al_c ++ ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] . charCodeAt ( 0 ) < 97 ) s = s . substring ( 0 , i ) + c [ nu_c ++ ] + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + c [ al_c ++ ] + s . substring ( i + 1 ) ; } return s ; } var s = " " ; document . write ( sort ( s ) ) ;