Reorder the position of the words in alphabetical order | Java implementation of the approach ; Function to print the ordering of words ; Creating list of words and assigning them index numbers ; Sort the list of words lexicographically ; Print the ordering ; Driver Code
import java . util . * ; class GFG { static void reArrange ( String words [ ] , int n ) { HashMap < String , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { freq . put ( words [ i ] , ( i + 1 ) ) ; } Arrays . sort ( words ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( freq . get ( words [ i ] ) + " ▁ " ) ; } public static void main ( String [ ] args ) { String words [ ] = { " live " , " place " , " travel " , " word " , " sky " } ; int n = words . length ; reArrange ( words , n ) ; } }