# Sort an array of strings according to string lengths

We are given an array of strings, we need to sort the array in increasing order of string lengths.

Examples:

```Input : {"GeeksforGeeeks", "I", "from", "am"}
Output : I am from GeeksforGeeks

Input :  {"You", "are", "beautiful", "looking"}
Output : You are looking beautiful
```

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

A simple solution is to write our own sort function that compares string lengths to decide which string should come first. Below is the implementation that uses Insertion Sort to sort the array.

## C++

 `// C++ program to sort an Array of  ` `// Strings according to their lengths ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the sorted array of string ` `void` `printArraystring(string,``int``); ` ` `  `// Function to Sort the array of string ` `// according to lengths. This function  ` `// implements Insertion Sort.  ` `void` `sort(string s[], ``int` `n) ` `{ ` `    ``for` `(``int` `i=1 ;i= 0 && temp.length() < s[j].length()) ` `        ``{ ` `            ``s[j+1] = s[j]; ` `            ``j--; ` `        ``} ` `        ``s[j+1] = temp; ` `    ``} ` `} ` `  `  `// Function to print the sorted array of string ` `void` `printArraystring(string str[], ``int` `n) ` `{ ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to sort an Array of  ` `// Strings according to their lengths ` `import` `java.util.*; ` ` `  `class` `solution ` `{ ` ` `  `// Function to print the sorted array of string ` `// void printArraystring(string,int); ` ` `  `// Function to Sort the array of string ` `// according to lengths. This function  ` `// implements Insertion Sort.  ` `static` `void` `sort(String []s, ``int` `n) ` `{ ` `    ``for` `(``int` `i=``1` `;i= ``0` `&& temp.length() < s[j].length()) ` `        ``{ ` `            ``s[j+``1``] = s[j]; ` `            ``j--; ` `        ``} ` `        ``s[j+``1``] = temp; ` `    ``} ` `} ` ` `  `// Function to print the sorted array of string ` `static` `void` `printArraystring(String str[], ``int` `n) ` `{ ` `    ``for` `(``int` `i=``0``; i

/div>

## C#

 `     `  `// C# program to sort an Array of  ` `// Strings according to their lengths ` `using` `System; ` `  `  `public` `class` `solution{ ` ` `  `    ``// Function to print the sorted array of string ` `    ``// void printArraystring(string,int); ` ` `  `    ``// Function to Sort the array of string ` `    ``// according to lengths. This function  ` `    ``// implements Insertion Sort.  ` `    ``static` `void` `sort(String []s, ``int` `n) ` `    ``{ ` `        ``for` `(``int` `i=1 ;i= 0 && temp.Length < s[j].Length) ` `            ``{ ` `                ``s[j+1] = s[j]; ` `                ``j--; ` `            ``} ` `            ``s[j+1] = temp; ` `        ``} ` `    ``} ` ` `  `    ``// Function to print the sorted array of string ` `    ``static` `void` `printArraystring(String []str, ``int` `n) ` `    ``{ ` `        ``for` `(``int` `i=0; i

Output:

``` I am from GeeksforGeeks
```

A better solution is to use sort function provided by programming languages like C++, Java. These functions also allow us to write our own custom comparator. Below is C++ implementation that uses C++ STL Sort function.

 `#include ` `using` `namespace`  `std; ` ` `  `// Function to check the small string ` `bool` `compare(string &s1,string &s2) ` `{ ` `    ``return` `s1.size() < s2.size(); ` `} ` ` `  `// Function to print the sorted array of string ` `void` `printArraystring(string str[], ``int` `n) ` `{ ` `    ``for` `(``int` `i=0; i

Output:

``` I am from GeeksforGeeks
```