Sort the given string using character search | Javascript implementation to sort the given string without using any sorting technique ; A character array to store the no . of occurrences of each character between ' a ' to ' z ' ; to store the final sorted string ; To store each occurrence of character by relative indexing ; To traverse the character array and append it to new_str ; Driver program to test above
function sortString ( str , n ) { var i ; var arr = Array ( 26 ) . fill ( 0 ) ; var new_str = " " ; for ( i = 0 ; i < n ; i ++ ) ++ arr [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; for ( i = 0 ; i < 26 ; i ++ ) while ( arr [ i ] -- ) new_str += String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; return new_str ; } var str = " " ; var n = str . length ; document . write ( sortString ( str , n ) ) ;