Sort the Array by reversing the numbers in it | Function to return the reverse of n ; Function to sort the array according to the reverse of elements ; Vector to store the reverse with respective elements ; Inserting reverse with elements in the vector pair ; Sort the vector , this will sort the pair according to the reverse of elements ; Print the sorted vector content ; Driver code
function reversDigits ( num ) { var rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = Math . floor ( num / 10 ) ; } return rev_num ; } function sortArr ( arr , n ) { var vp = new Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { vp [ i ] = [ ] ; } for ( var i = 0 ; i < n ; i ++ ) { var pair = [ ] ; pair . push ( reversDigits ( arr [ i ] ) ) ; pair . push ( arr [ i ] ) ; vp [ i ] = pair ; } vp = vp . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] ; } ) ; 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 ) ;