Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Last Updated : 03 Aug, 2022
View Discussion
Improve Article
Save Article

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:

Below is the implementation of the above approach.

C++




// 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 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[0]);
    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))
 
    # Print the 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




<script>
 
// Javascript program of the above approach
 
// Function to sort the strings in
// descending order and sort the
// array in ascending order
function 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)=>{return b>a?1:-1}).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 Code
var s = [ "apple", "box", "cat" ];
var N = s.length;
sortStr(s, N);
 
</script>

Output

pplea tca xob 

Time Complexity: O(N*MlogM) where N is the size of the array and M is the maximum size of any string present in the array.
Auxiliary Space: O(1) 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!