Related Articles

# Sort an array of strings in ascending order with each string sorted in descending order

• Last Updated : 12 May, 2021

Given a array of strings S[] of size N (1 ≤ N ≤ 105), sort characters of each string in descending order and then print the array of strings in ascending order.

Examples:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: s[] = {“apple”, “box”, “cat”}
Output: pplea tca xob
Explanation:
Sorting each string in descending order, S[] modfies to {“pplea”, “xob”, “tca”}.
Sorting the array in ascending order modifies S[] to {pplea, tca, xob}.

Input: s[] = {“pqr”, “moon”, “geeks”}
Output: oonm rqp skgee

Approach: Follow the steps below to solve the problem:

Below is the implementation of the above approach.

## C++

 `// C++ program of the above approach` `#include ``using` `namespace` `std;` `// Function to sort the strings in``// descending order and sort the``// array in ascending order``void` `sortStr(string s[], ``int` `N)``{` `    ``// Traverse the array of strings``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Sort each string in descending order``        ``sort(s[i].begin(), s[i].end(),``             ``greater<``char``>());``    ``}` `    ``// Sort the array in ascending order``    ``sort(s, s + N);` `    ``// Print the array of strings``    ``for` `(``int` `i = 0; i < N; i++) {``        ``cout << s[i] << ``" "``;``    ``}``}` `// Driver Code``int` `main()``{``    ``string s[] = { ``"apple"``, ``"box"``, ``"cat"` `};``    ``int` `N = ``sizeof``(s) / ``sizeof``(s);``    ``sortStr(s, N);` `    ``return` `0;``}`

## Java

 `// Java program of the above approach``import` `java.util.Arrays;``class` `GFG{` `  ``// Function to sort the Strings in``  ``// descending order and sort the``  ``// array in ascending order``  ``static` `void` `sortStr(String s[], ``int` `N)``  ``{` `    ``// Traverse the array of Strings``    ``for` `(``int` `i = ``0``; i < N; i++)``    ``{` `      ``// Sort each String in descending order       ``      ``s[i] = reverse(sortString(s[i]));``    ``}` `    ``// Sort the array in ascending order``    ``Arrays.sort(s);` `    ``// Print the array of Strings``    ``for` `(``int` `i = ``0``; i < N; i++)``    ``{``      ``System.out.print(s[i]+ ``" "``);``    ``}``  ``}``  ``static` `String sortString(String inputString)``  ``{` `    ``// convert input string to char array``    ``char` `tempArray[] = inputString.toCharArray();` `    ``// sort tempArray``    ``Arrays.sort(tempArray);` `    ``// return new sorted string``    ``return` `new` `String(tempArray);``  ``}``  ``static` `String reverse(String input)``  ``{``    ``char``[] a = input.toCharArray();``    ``int` `l, r = a.length - ``1``;``    ``for` `(l = ``0``; l < r; l++, r--)``    ``{``      ``char` `temp = a[l];``      ``a[l] = a[r];``      ``a[r] = temp;``    ``}``    ``return` `String.valueOf(a);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``String s[] = { ``"apple"``, ``"box"``, ``"cat"` `};``    ``int` `N = s.length;``    ``sortStr(s, N);``  ``}``}` `// This code is contributed by shikhasingrajput`

## Python3

 `# Python program of the above approach` `# Function to sort the Strings in``# descending order and sort the``# array in ascending order``def` `sortStr(s, N):``  ` `    ``# Traverse the array of Strings``    ``for` `i ``in` `range``(N):``      ` `        ``# Sort each String in descending order``        ``s[i] ``=` `"``".join(reversed("``".join(``sorted``(s[i])))) ;``        ` `    ``# Sort the array in ascending order``    ``s ``=` `" "``.join(``sorted``(s))` `    ``# Prthe array of Strings``    ``print``(s)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``s ``=` `[``"apple"``, ``"box"``, ``"cat"``];``    ``N ``=` `len``(s);``    ``sortStr(s, N);` `    ``# This code is contributed by shikhasingrajput`

## C#

 `// C# program of the above approach``using` `System;``class` `GFG {` `  ``static` `void` `reverse(``char``[] a)``  ``{``    ``int` `i, n = a.Length;``    ``char` `t;``    ``for` `(i = 0; i < n / 2; i++)``    ``{``      ``t = a[i];``      ``a[i] = a[n - i - 1];``      ``a[n - i - 1] = t;``    ``}``  ``}` `  ``// Function to sort the strings in``  ``// descending order and sort the``  ``// array in ascending order``  ``static` `void` `sortStr(``string``[] s, ``int` `N)``  ``{` `    ``// Traverse the array of strings``    ``for` `(``int` `i = 0; i < N; i++)``    ``{``      ``char``[] t = s[i].ToCharArray();` `      ``// Sort each string``      ``Array.Sort(t);` `      ``// Reverse the string``      ``reverse(t);``      ``s[i] = String.Join(``""``, t);``    ``}` `    ``// Sort the array in ascending order``    ``Array.Sort(s);` `    ``// Print the array of strings``    ``for` `(``int` `i = 0; i < N; i++)``    ``{``      ``Console.Write(s[i] + ``" "``);``    ``}``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main()``  ``{``    ``string``[] s = { ``"apple"``, ``"box"``, ``"cat"` `};``    ``int` `N = s.Length;``    ``sortStr(s, N);``  ``}``}` `// This code is contributed by subhammahato348`

## Javascript

 ``

Output:
`pplea tca xob`

Time Complexity: O(NlogN)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up