# Reorder the position of the words in alphabetical order

Given an array arr[] of strings, the task is to reorder the strings lexicographically and print their positions in the original list.

Examples:

Input: arr[] = {“zxc”, “efg”, “jkl”}
Output: 2 3 1
Sorted list will be {“efg”, “jkl”, “zxc”} and their
original positions were 2, 3 and 1 respectively.

Input: arr[] = {“live”, “place”, “travel”, “word”, “sky”}
Output: 1 2 5 3 4

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Assign all the words an integer number equal to their position in the array. Then sort the list of words lexicographically and there positions are altered, and therefore there positions are printed starting from the first word in the sorted list.

Below is the implementation of the above approach:

## C++

 `// CPP implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the ordering of words ` `void` `reArrange(string words[], ``int` `n) ` `{ ` ` `  `    ``// Creating list of words and assigning ` `    ``// them index numbers ` `    ``map mp; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``mp[words[i]] = i + 1; ` ` `  `    ``// Sort the list of words ` `    ``// lexicographically ` `    ``sort(words, words + n); ` ` `  `    ``// Print the ordering ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << mp[words[i]] << ``" "``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``string words[] = { ``"live"``, ``"place"``, ``"travel"``, ``"word"``, ``"sky"` `}; ` `    ``int` `n = ``sizeof``(words) / ``sizeof``(words[0]); ` `    ``reArrange(words, n); ` `} ` ` `  `// This code is contributed by ` `// Surendra_Gangwar `

## Java

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

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to print the ordering of words  ` `def` `reArrange(words, n): ` `    ``# Creating list of words and assigning  ` `    ``# them index numbers ` `    ``mp ``=` `{} ` `    ``for` `i ``in` `range``(n): ` `        ``mp[words[i]] ``=` `i ``+` `1` ` `  `    ``# Sort the list of words ` `    ``# lexicographically ` `    ``words.sort(); ` ` `  `    ``# Print the ordering ` `    ``for` `i ``in` `range``(n): ` `        ``print``(mp[words[i]], end ``=` `" "``)  ` ` `  `# Driver Code  ` ` `  `words ``=` `[ ``"live"``, ``"place"``, ``"travel"``, ``"word"``, ``"sky"` `]  ` `n ``=` `len``(words)  ` `reArrange(words, n);  ` ` `  `# This code is contributed by  ` `# Rajnis09  `

## C#

 `// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG { ` ` `  `    ``// Function to print the ordering of words ` `    ``static` `void` `reArrange(String[] words, ``int` `n) ` `    ``{ ` ` `  `        ``// Creating list of words and assigning ` `        ``// them index numbers ` `        ``Dictionary freq = ``new` `Dictionary(); ` `        ``for` `(``int` `i = 0; i < n; i++) { ` `            ``freq.Add(words[i], (i + 1)); ` `        ``} ` ` `  `        ``// Sort the list of words ` `        ``// lexicographically ` `        ``Array.Sort(words); ` ` `  `        ``// Print the ordering ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``Console.Write(freq[words[i]] + ``" "``); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``String[] words = { ``"live"``, ``"place"``, ``"travel"``, ``"word"``, ``"sky"` `}; ` `        ``int` `n = words.Length; ` `        ``reArrange(words, n); ` `    ``} ` `} ` ` `  `// This code contributed by Rajput-Ji `

## PHP

 ` `

Output:

```1 2 5 3 4
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.