Posted on by Kalkicode
Code Core

Sort an array of strings

Sorting an array of strings means arranging the strings in a specific order, typically either in ascending or descending order based on a chosen sorting algorithm. The sorting algorithm compares pairs of strings and swaps their positions until the array is ordered according to the specified criteria. For example, sorting an array of names in alphabetical order would arrange the names from A to Z, while sorting in reverse alphabetical order would arrange the names from Z to A.

Here given code implementation process.

/*
    Java program for
    Sort an array of strings
*/
import java.util.Arrays;
public class Arrangement
{
    public void displayArray(String[] arr, int n)
    {
        for (int i = 0; i < n; ++i)
        {
            System.out.print(" " + arr[i]);
        }
        System.out.print("\n");
    }
    public void sortStringArray(String[] arr, int n)
    {
        System.out.print(" Before Sort : ");
        displayArray(arr, n);
        // Sort string array
        Arrays.sort(arr);
        System.out.print(" After Sort  : ");
        displayArray(arr, n);
    }
    public static void main(String[] args)
    {
        Arrangement task = new Arrangement();
        String[] arr = {
            "Blueberries" , "Apricots" , "Mango" , 
            "Avocado" , "Cherries" , "Grapes"
        };
        int n = arr.length;
        task.sortStringArray(arr, n);
    }
}

Output

 Before Sort :  Blueberries Apricots Mango Avocado Cherries Grapes
 After Sort  :  Apricots Avocado Blueberries Cherries Grapes Mango
// Include header file
#include <iostream>
#include <algorithm>

using namespace std;
class Arrangement
{
	public: void displayArray(string arr[], int n)
	{
		for (int i = 0; i < n; ++i)
		{
			cout << " " << arr[i];
		}
		cout << "\n";
	}
	void sortStringArray(string arr[], int n)
	{
		cout << " Before Sort : ";
		this->displayArray(arr, n);
		// Sort string array
		sort(arr, arr + n);
		cout << " After Sort  : ";
		this->displayArray(arr, n);
	}
};
int main()
{
	Arrangement *task = new Arrangement();
	string arr[] = {
		"Blueberries" , "Apricots" , "Mango" , "Avocado" , 
      	"Cherries" , "Grapes"
	};
	int n = sizeof(arr) / sizeof(arr[0]);
	task->sortStringArray(arr, n);
	return 0;
}

Output

 Before Sort :  Blueberries Apricots Mango Avocado Cherries Grapes
 After Sort  :  Apricots Avocado Blueberries Cherries Grapes Mango
// Include namespace system
using System;
public class Arrangement
{
	public void displayArray(String[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
		Console.Write("\n");
	}
	public void sortStringArray(String[] arr, int n)
	{
		Console.Write(" Before Sort : ");
		this.displayArray(arr, n);
		// Sort string array
		Array.Sort(arr);
		Console.Write(" After Sort  : ");
		this.displayArray(arr, n);
	}
	public static void Main(String[] args)
	{
		Arrangement task = new Arrangement();
		String[] arr = {
			"Blueberries" , "Apricots" , "Mango" , 
          	"Avocado" , "Cherries" , "Grapes"
		};
		int n = arr.Length;
		task.sortStringArray(arr, n);
	}
}

Output

 Before Sort :  Blueberries Apricots Mango Avocado Cherries Grapes
 After Sort  :  Apricots Avocado Blueberries Cherries Grapes Mango
package main
import "sort"
import "fmt"
func displayArray(arr[] string, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", arr[i])
	}
	fmt.Print("\n")
}
func sortStringArray(arr[] string, n int) {
	fmt.Print(" Before Sort : ")
	displayArray(arr, n)
	// Sort string array
	sort.Strings(arr)
	fmt.Print(" After Sort  : ")
	displayArray(arr, n)
}
func main() {

	var arr = [] string {"Blueberries",
		"Apricots",
		"Mango",
		"Avocado",
		"Cherries",
		"Grapes"}
	var n int = len(arr)
	sortStringArray(arr, n)
}

Output

 Before Sort :  Blueberries Apricots Mango Avocado Cherries Grapes
 After Sort  :  Apricots Avocado Blueberries Cherries Grapes Mango
<?php
class Arrangement
{
	public	function displayArray($arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
		echo("\n");
	}
	public	function sortStringArray(&$arr, $n)
	{
		echo(" Before Sort : ");
		$this->displayArray($arr, $n);
		// Sort string array
		sort($arr);
		echo(" After Sort  : ");
		$this->displayArray($arr, $n);
	}
}

function main()
{
	$task = new Arrangement();
	$arr = array("Blueberries", "Apricots", "Mango", 
                 "Avocado", "Cherries", "Grapes");
	$n = count($arr);
	$task->sortStringArray($arr, $n);
}
main();

Output

 Before Sort :  Blueberries Apricots Mango Avocado Cherries Grapes
 After Sort  :  Apricots Avocado Blueberries Cherries Grapes Mango
class Arrangement
{
	displayArray(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write("\n");
	}
	sortStringArray(arr, n)
	{
		process.stdout.write(" Before Sort : ");
		this.displayArray(arr, n);
		// Sort string array
		arr.sort();
		process.stdout.write(" After Sort  : ");
		this.displayArray(arr, n);
	}
}

function main()
{
	var task = new Arrangement();
	var arr = ["Blueberries", "Apricots", "Mango", 
               "Avocado", "Cherries", "Grapes"];
	var n = arr.length;
	task.sortStringArray(arr, n);
}
main();

Output

 Before Sort :  Blueberries Apricots Mango Avocado Cherries Grapes
 After Sort  :  Apricots Avocado Blueberries Cherries Grapes Mango
class Arrangement :
	def displayArray(self, arr, n) :
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	def sortStringArray(self, arr, n) :
		print(" Before Sort : ", end = "")
		self.displayArray(arr, n)
		#  Sort string array
		arr.sort()
		print(" After Sort  : ", end = "")
		self.displayArray(arr, n)
	

def main() :
	task = Arrangement()
	arr = ["Blueberries", "Apricots", "Mango", "Avocado", "Cherries", "Grapes"]
	n = len(arr)
	task.sortStringArray(arr, n)

if __name__ == "__main__": main()

Output

 Before Sort :   Blueberries  Apricots  Mango  Avocado  Cherries  Grapes
 After Sort  :   Apricots  Avocado  Blueberries  Cherries  Grapes  Mango
class Arrangement 
	def displayArray(arr, n) 
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

		print("\n")
	end

	def sortStringArray(arr, n) 
		print(" Before Sort : ")
		self.displayArray(arr, n)
		#  Sort string array
		arr = arr.sort
		print(" After Sort  : ")
		self.displayArray(arr, n)
	end

end

def main() 
	task = Arrangement.new()
	arr = ["Blueberries", "Apricots", "Mango", 
           "Avocado", "Cherries", "Grapes"]
	n = arr.length
	task.sortStringArray(arr, n)
end

main()

Output

 Before Sort :  Blueberries Apricots Mango Avocado Cherries Grapes
 After Sort  :  Apricots Avocado Blueberries Cherries Grapes Mango
import scala.collection.mutable._;
class Arrangement()
{
	def displayArray(arr: Array[String], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
		print("\n");
	}
	def sortStringArray(arr: Array[String], n: Int): Unit = {
		print(" Before Sort : ");
		displayArray(arr, n);
		// Sort string array
		scala.util.Sorting.quickSort(arr)
		print(" After Sort  : ");
		displayArray(arr, n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Arrangement = new Arrangement();
		var arr: Array[String] = Array("Blueberries", "Apricots", "Mango",
                                       "Avocado", "Cherries", "Grapes");
		var n: Int = arr.length;
		task.sortStringArray(arr, n);
	}
}

Output

 Before Sort :  Blueberries Apricots Mango Avocado Cherries Grapes
 After Sort  :  Apricots Avocado Blueberries Cherries Grapes Mango
import Foundation;
class Arrangement
{
	func displayArray(_ arr: [String], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	func sortStringArray(_ arr: inout[String], _ n: Int)
	{
		print(" Before Sort : ", terminator: "");
		self.displayArray(arr, n);
		// Sort string array
		arr = arr.sorted();
		print(" After Sort  : ", terminator: "");
		self.displayArray(arr, n);
	}
}
func main()
{
	let task: Arrangement = Arrangement();
	var arr: [String] = ["Blueberries", "Apricots", "Mango", "Avocado", "Cherries", "Grapes"];
	let n: Int = arr.count;
	task.sortStringArray(&arr, n);
}
main();

Output

 Before Sort :   Blueberries  Apricots  Mango  Avocado  Cherries  Grapes
 After Sort  :   Apricots  Avocado  Blueberries  Cherries  Grapes  Mango
class Arrangement
{
	fun displayArray(arr: Array < String > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	fun sortStringArray(arr: Array < String > , n: Int): Unit
	{
		print(" Before Sort : ");
		this.displayArray(arr, n);
		// Sort string array
		arr.sort();
		print(" After Sort  : ");
		this.displayArray(arr, n);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Arrangement = Arrangement();
	val arr: Array < String > = arrayOf("Blueberries", "Apricots", 
                                        "Mango", "Avocado", "Cherries", "Grapes");
	val n: Int = arr.count();
	task.sortStringArray(arr, n);
}

Output

 Before Sort :  Blueberries Apricots Mango Avocado Cherries Grapes
 After Sort  :  Apricots Avocado Blueberries Cherries Grapes Mango

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment