Sorting the array of strings

StringViews 1378

Given an array which contains strings, this function will sort the array. This can be clearly shown in below example



S[] = {“Abhishek”, “Zeroess”,”Hello”,”Amit”,”Bananan”,”Problem”,”Ankush”,”Ahmed”}


Abhishek  Ahmed  Amit  Ankush  Bananan  Hello  Problem  Zeroess

In the above example we can see that the strings in the array are sorted in alphabetical order


In this algorithm we use the standard template library function. We use the function named stable_sort to sort the input strings in the array ie, stable_sort(S,S+N) where N is the size of the array stable_sort function directly sorts the strings in the array.

C++ Program

#include <bits/stdc++.h>
using namespace std;
#define MAX 10005

int main()
	string S[] = {"Abhishek", "Zeroess","Hello","Amit","Bananan","Problem","Ankush","Ahmed"};
	int N = sizeof(S)/sizeof(S[0]);
	for(int i=0;i<N;i++)
		cout<<S[i]<<"  ";
	return 0;

Try It

Translate »