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

21 Nov, 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:  
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:
Traverse the array.
Sort each string in descending order.q c2
After sorting each string, sort the array of strings in ascending order.
Print the sorted array of strings.
Below is the implementation of the above approach.
C++
Java
Python3
C#
Javascript
// C++ program of the above approach #include <bits/stdc++.h>using namespace std; // Function to sort the strings in// descending order and sort the// array in ascending ordervoid 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 Codeint main(){    string s[] = { "apple", "box", "cat" };    int N = sizeof(s) / sizeof(s[0]);    sortStr(s, N);     return 0;}
// Java program of the above approachimport 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
# Python program of the above approach # Function to sort the Strings in# descending order and sort the# array in ascending orderdef 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))     # Print the array of Strings    print(s) # Driver Codeif __name__ == '__main__':    s = ["apple", "box", "cat"];    N = len(s);    sortStr(s, N);     # This code is contributed by shikhasingrajput
// C# program of the above approachusing 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
<script> // Javascript program of the above approach // Function to sort the strings in// descending order and sort the// array in ascending orderfunction sortStr( s, N){     // Traverse the array of strings    for (var i = 0; i < N; i++) {         // Sort each string in descending order        s[i] = s[i].split('').sort((a,b)=>b>a).join('')    }     // Sort the array in ascending order    s.sort();     // Print the array of strings    for (var i = 0; i < N; i++) {        document.write( s[i] + " ");    }} // Driver Codevar s = [ "apple", "box", "cat" ];var N = s.length;sortStr(s, N); </script>
  
pplea tca xob
 
Time Complexity: O(NlogN) Auxiliary Space: O(1) 
subhammahato348
shikhasingrajput
itsok
Kirti_Mangal
array-rearrange
Arrays
Sorting
Strings
Arrays
Strings
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java