Sorting a dynamic 2-dimensional array of Strings
Last Updated :
27 Mar, 2023
Prerequisite: How to dynamically allocate a 2D array in C?
Double pointer: A pointer pointing to another pointer is known as a Double pointer. To represent the double pointer ' ** ' is used. Double pointer is also called as pointer to pointer. Example:
Input: Geeks, Gfg, Placement, Sudo, Gate
Output: Gate, Geeks, Gfg, Placement, Sudo
The idea is to dynamically allocate memory and values to the strings in a form of a 2-D array. Then apply bubble sort using strcmp and strcpy function.
Implementation:
C++
#include <cstdlib>
#include <cstring>
#include <iostream>
// Function to sort the values
void sort(char** names, int n)
{
int i, j;
// Perform sort operation using bubble sort
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (strcmp(names[j], names[j + 1]) > 0) {
char* temp;
temp = (char*)calloc(30, sizeof(char));
strcpy(temp, names[j]);
strcpy(names[j], names[j + 1]);
strcpy(names[j + 1], temp);
}
}
// Driver code
int main()
{
char** names;
int n, i;
std::cout
<< "Enter the number of names to be printed: ";
std::cin >> n;
// allocating memory for 1st dimension
names = (char**)calloc(n, sizeof(char*));
for (i = 0; i < n; i++)
// allocating memory for 2nd dimension
{
names[i] = (char*)calloc(30, sizeof(char));
std::cin >> names[i];
}
sort(names, n);
std::cout << "\nArray after sorting:\n";
for (i = 0; i < n; i++)
std::cout << names[i] << std::endl;
return 0;
}
//code contributed by dhanshriborse
C
// C program to sort an array of strings
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to sort the values
void sort(char** names, int n)
{
int i, j;
// Perform sort operation using bubble sort
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (strcmp(names[j], names[j + 1]) > 0) {
char* temp;
temp = (char*)calloc(30, sizeof(char));
strcpy(temp, names[j]);
strcpy(names[j], names[j + 1]);
strcpy(names[j + 1], temp);
}
}
// Driver code
int main()
{
char** names;
int n, i;
printf("Enter the number of names to be printed: ");
scanf("%d\n", &n);
// allocating memory for 1st dimension
names = (char**)calloc(n, sizeof(char*));
for (i = 0; i < n; i++)
// allocating memory for 2nd dimension
{
names[i] = (char*)calloc(30, sizeof(char));
scanf("%s", names[i]);
}
sort(names, n);
printf("\nArray after sorting:\n");
for (i = 0; i < n; i++)
printf("%s\n", names[i]);
return 0;
}
Java
// Java code
import java.util.Scanner;
// Function to sort the values
class GFG
{
public static void sort(String names[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (names[j].compareTo(names[j + 1]) > 0)
{
// swap arr[j+1] and arr[i]
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}
}
// Driver code
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
Sort ob = new Sort();
String names[] = new String[10];
int n = scanner.nextInt();
scanner.nextLine();
// Input names
for(int i = 0; i < n; i++){
names[i] = scanner.nextLine();
}
ob.sort(names, n);
System.out.println("\nArray after sorting:\n");
for(int i = 0; i < n; i++){
System.out.println(names[i]);
}
}
}
// This code is contributed by akashish__
Python3
# Python code
# Function to sort the values
def sort(names, n):
for i in range(n-1):
for j in range(n-i-1):
if (names[j] > names[j+1]):
temp = names[j]
names[j] = names[j+1]
names[j+1] = temp
# Driver code
if __name__ == '__main__':
names = []
n = int(input("Enter the number of names to be printed: "))
for i in range(0, n):
names.append(input())
sort(names, n)
print("\nArray after sorting:\n")
for i in range(n):
print(names[i])
# This code is contributed by akashish__
JavaScript
// JavaScript code
// Function to sort the values
function sort(names, n){
for(let i=0; i<n-1; i++){
for(let j=0; j<n-i-1; j++){
if(names[j] > names[j+1]){
let temp = names[j];
names[j] = names[j+1];
names[j+1] = temp;
}
}
}
}
// Driver code
let names = [];
let n = parseInt(prompt("Enter the number of names to be printed: "));
for(let i=0; i<n; i++){
names.push(prompt());
}
sort(names, n);
console.log("\nArray after sorting:\n");
for(let i=0; i<n; i++){
console.log(names[i]);
}
// This code is contributed by akashish__
C#
using System;
class Program {
static void Main(string[] args) {
string[] names;
int n, i;
Console.Write("Enter the number of names to be printed: ");
string input = Console.ReadLine();
while (!int.TryParse(input, out n)) {
Console.Write("Invalid input. Please enter a valid integer: ");
input = Console.ReadLine();
}
// allocating memory for 1st dimension
names = new string[n];
for (i = 0; i < n; i++) // allocating memory for 2nd dimension
{
names[i] = Console.ReadLine();
}
Sort(names, n);
Console.WriteLine("\nArray after sorting:");
for (i = 0; i < n; i++)
Console.WriteLine(names[i]);
}
static void Sort(string[] names, int n) {
int i, j;
// Perform sort operation using bubble sort
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (string.Compare(names[j], names[j + 1]) > 0) {
string temp;
temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}
Output:

Similar Reads
Sort an Array of Strings in Lexicographical order Given an array of strings arr[] of size n, the task is to sort all the strings in lexicographical order. Examples:Input: arr[] = ["banana", "apple", "cherry"]Output: ["apple", "banana", "cherry"]Explanation: All strings are sorted alphabetically. "apple" comes before "banana", and "banana" before "c
11 min read
How to sort an Array of Strings in Java Array Of StringsTo sort an array of strings in Java, we can use Arrays.sort() function. Java // A sample Java program to // sort an array of strings // in ascending and descending // orders using Arrays.sort(). import java.util.Arrays; import java.util.Collections; // Driver Class public class SortE
3 min read
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 A simple solution is to write our
10 min read
How to Sort a Multi-dimensional Array by Value What is Sorting?Arranging data in an increasing or decreasing fashion according to their values is called Sorting. Below are shown some processes for sorting arrays of several dimensions. Sorting a 1-Dimensional array:We can sort any Dimensional array using the sort method in C++. Syntax: sort(arr,
11 min read
Sort an array of Strings according frequency Given an array of strings arr[], the task is to sort the array of strings according to the frequency of each string, in ascending order. If two elements have the same frequency, then they are sorted into lexicographical order. Examples: Input: arr[] = {"Geeks", "for", "Geeks"} Output: {"for", "Geeks
8 min read