Sort the numbers according to their product of digits | Function to return the product of the digits of n ; Function to sort the array according to the product of the digits of elements ; Vector to store the digit product with respective elements ; Inserting digit product with elements in the vector pair ; Sort the vector , this will sort the pair according to the product of the digits ; Print the sorted vector content ; Driver code
function productOfDigit ( n ) { var product = 1 ; while ( n > 0 ) { product *= n % 10 ; n = Math . floor ( n / 10 ) ; } return product ; } function sortArr ( arr , n ) { var vp = new Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { vp [ i ] . push ( productOfDigit ( arr [ i ] ) ) ; vp [ i ] . push ( arr [ i ] ) ; } vp . sort ( ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( vp [ i ] [ 1 ] + " " ) ; } var arr = [ 12 , 10 , 102 , 31 , 15 ] ; var n = arr . length ; sortArr ( arr , n ) ;