Given an array of strings, we need to sort the array in increasing order of string lengths, using Map Data Structure.

**Examples:**

Input:str[] = {“GeeksforGeeeks”, “I”, “from”, “am”}

Output:I am from GeeksforGeeks

Input:str[] = {“You”, “are”, “beautiful”, “looking”}

Output:You are looking beautiful

**Approach:** The given approach is for Java using TreeMap

- Take a TreeMap which will contain the length of the string as the key and an ArrayList of Strings that has the same length that of the key.
- Since we know that a TreeMap is already sorted so after inserting into the TreeMap we again fetch the values(the Strings from the ArrayList) one by one into the Strings Array.

Below is the implementation of the above approach:

`// Java program to sort an Array of ` `// Strings according to their lengths ` `// using Map ` ` ` `import` `java.util.*; ` `import` `java.util.Map.Entry; ` `import` `java.io.*; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Function to Sort the array of string ` ` ` `// according to lengths using Map ` ` ` `static` `String[] sort(String[] str, ` `int` `n) ` ` ` `{ ` ` ` `TreeMap<Integer, ArrayList<String> > map ` ` ` `= ` `new` `TreeMap<Integer, ArrayList<String> >(); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` ` ` `map.putIfAbsent(str[i].length(), ` ` ` `new` `ArrayList<String>()); ` ` ` `map.get(str[i].length()).add(str[i]); ` ` ` `} ` ` ` ` ` `int` `temp = ` `0` `; ` ` ` ` ` `for` `(Entry<Integer, ArrayList<String> > ` ` ` `e : map.entrySet()) { ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < e.getValue().size(); i++) { ` ` ` ` ` `str[temp] = e.getValue().get(i); ` ` ` `temp++; ` ` ` `} ` ` ` `} ` ` ` `return` `str; ` ` ` `} ` ` ` ` ` `// Function to print the sorted array of string ` ` ` `static` `void` `printArraystring(String str[], ` `int` `n) ` ` ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `System.out.print(str[i] + ` `" "` `); ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `String[] arr = { ` `"GeeksforGeeks"` `, ` ` ` `"I"` `, ` `"from"` `, ` `"am"` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `// Function to perform sorting ` ` ` `arr = sort(arr, n); ` ` ` ` ` `// Calling the function to print result ` ` ` `printArraystring(arr, n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

I am from GeeksforGeeks

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sort an array of strings according to string lengths
- Sort the array of strings according to alphabetical order defined by another string
- Sort an Array of Strings according to the number of Vowels in them
- Sort an array of Strings according frequency
- Sort a string according to the order defined by another string
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Sort an array according to absolute difference with a given value "using constant extra space"
- Sort an array according to absolute difference with given value using Functors
- Program to sort an array of strings using Selection Sort
- Sort an array according to the order defined by another array
- XOR two binary strings of unequal lengths
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Sort a string according to the frequency of characters
- Sort an array according to count of set bits
- Sort an array according to absolute difference with given value
- Python | Sort an array according to absolute difference
- Sort an array according to the increasing count of distinct Prime Factors
- Sort an array according to count of set bits | Set 2
- Sort integers in array according to their distance from the element K

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.