Sort the Array by reversing the numbers in it | Java implementation of the approach ; 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
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int reversDigits ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static void sortArr ( int arr [ ] , int n ) { ArrayList < int [ ] > vp = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { vp . add ( new int [ ] { reversDigits ( arr [ i ] ) , arr [ i ] } ) ; } Collections . sort ( vp , ( a , b ) -> a [ 0 ] - b [ 0 ] ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) System . out . print ( vp . get ( i ) [ 1 ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 10 , 102 , 31 , 15 } ; int n = arr . length ; sortArr ( arr , n ) ; } }