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


In this article, we dive into a unique and interesting problem related to arrays and string manipulation in various programming languaues. The problem at hand is "Sort an array of strings in ascending order with each string sorted in descending order". This problem is an excellent way to enhance your knowledge of string manipulation, arrays, and sorting algorithms.

Problem Statement

Given an array of strings, the task is to sort the array in ascending order, but with each string sorted in descending order.

Solution Approach

We can solve this problem by using the sort function provided by the C++ Standard Library. First, we'll sort each individual string in descending order. After that, we'll sort the entire array of strings in ascending order.

Example

Here're the programs that implements the above solution −

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sortArray(char** arr, int size) {
   for (int i = 0; i < size; i++) {
      int len = strlen(arr[i]);
      for (int j = 0; j < len - 1; j++) {
         for (int k = j + 1; k < len; k++) {
            if (arr[i][j] < arr[i][k]) {
               char temp = arr[i][j];
               arr[i][j] = arr[i][k];
               arr[i][k] = temp;
            }
         }
      }
   }
    
   for (int i = 0; i < size - 1; i++) {
      for (int j = i + 1; j < size; j++) {
         if (strcmp(arr[i], arr[j]) > 0) {
            char* temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
         }
      }
   }
}
int main() {
   char* arr[] = {
      malloc(sizeof(char) * (4 + 1)), // "acb" + null terminator
      malloc(sizeof(char) * (4 + 1)), // "bca" + null terminator
      malloc(sizeof(char) * (4 + 1))  // "abc" + null terminator
   };

   strcpy(arr[0], "acb");
   strcpy(arr[1], "bca");
   strcpy(arr[2], "abc");

   int size = sizeof(arr) / sizeof(arr[0]);

   sortArray(arr, size);

   printf("The sorted array is: ");
   for (int i = 0; i < size; i++) {
      printf("%s ", arr[i]);
      free(arr[i]); // free the allocated memory for each string
   }
   printf("\n");
   return 0;
}

Output

The sorted array is: cba cba cba
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

void sortArray(vector<string>& arr) {
   for(string& str : arr) {
      sort(str.begin(), str.end(), greater<char>());
   }
   sort(arr.begin(), arr.end());
}

int main() {
   vector<string> arr = {"acb", "bca", "abc"};
   sortArray(arr);
   cout << "The sorted array is: ";
   for(const string& str : arr) {
      cout << str << " ";
   }
   cout << endl;
   return 0;
}	  

Output

The sorted array is: cba cba cba 
import java.util.Arrays;

public class Main {
   public static void sortArray(String[] arr) {
      for (int i = 0; i < arr.length; i++) {
         char[] charArray = arr[i].toCharArray();
         Arrays.sort(charArray);
         arr[i] = new StringBuilder(new String(charArray)).reverse().toString();
      }

      Arrays.sort(arr);
   }

   public static void main(String[] args) {
      String[] arr = {"acb", "bca", "abc"};
      sortArray(arr);
      System.out.print("The sorted array is: ");
      for (String str : arr) {
         System.out.print(str + " ");
      }
      System.out.println();
   }
}

Output

The sorted array is: cba cba cba
def sort_array(arr):
   for i in range(len(arr)):
      arr[i] = ''.join(sorted(arr[i], reverse=True))
   
   arr.sort()

arr = ["acb", "bca", "abc"]
sort_array(arr)
print("The sorted array is:", ' '.join(arr))

Output

The sorted array is: cba cba cba

Explanation with a Test Case

Let's consider the array of strings {"acb", "bca", "abc"}.

When we pass this array to the sortArray function, it first sorts each string in descending order. The array becomes {"cba", "cba", "cba"}.

Then it sorts the array of strings in ascending order. However, since all the strings are the same, the sorted array remains {"cba", "cba", "cba"}.

Conclusion

This problem presents a unique use case of the sort() function. It's an excellent problem to practice your coding skills and to understand how to sort strings and arrays.

Updated on: 27-Oct-2023

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements