C++ Sorting Strings by writing a Custom Sort Method
In this tutorial, we will learn how to sort Strings on the basis of length using a Custom Sort Method and its implementation in the C++ programming language.
What is a Set?
In programming, a Set is used to store unique values of a list and also automatically providing an ordering to its elements. By default, the ordering is in ascending order.
The elements are inserted using the
insert() method. If the same value is inserted multiple times, the set automatically deletes the duplicates and only store the single copy of that element.
The elements of the Set are deleted using the
What is an Unordered Set?
An Unordered Set also stores only the single copy of the elements by removing the duplicates but does not sort the elements automatically as in Set.
An Unordered Set internally uses a Hash Table and hence the ordering is random depending upon the internally used Hash function.
Custom Sort Method:
Whenever we need to explicitly determine the condition for sorting, we need to create this method to define the logic.
For a better understanding of its implementation, refer to the well-commented CPP code given below.
using namespace std;
//Returns true if first string is of longer length than second
bool cmp(string x, string y)
int n = x.length();
int m = y.length();
if (n > m)
//Function to print the elements of the unordered set using an iterator
void show(unordered_set<string> s)
//declaring an iterator to iterate through the unordered set
for (i = s.begin(); i != s.end(); i++)
cout << *i << " "; //accessing the elements of the unordered set using * as i stores the address to each element
cout << endl;
cout << "\n\nWelcome to Studytonight :-)\n\n\n";
cout << " ===== Program to demonstrate the Sorting Strings on the basis of length, in CPP ===== \n\n\n\n";
cout << " *** Unordered Set automatically removes the duplicate elements and maintains a random ordering. *** \n\n";
cout << " *** This random ordering depends on the hash function that is used internally. *** \n\n";
cout << " *** Unordered set can be sorted by copying its elements to a Vector. *** \n\n";
//Unordered Set declaration (Unordered Set of strings)
//Filling the elements by using the insert() method.
cout << "\n\nFilling the Unordered Set with strings in random order."; //Unlike Set, this is not automatically sorted
cout << "\n\nThe elements of the Unordered Set before sorting are:\n ";
//Declaring a vector and initializing it with the elements of the unordered set
vector<string> v(s.begin(), s.end());
//Sorting the vector elements in descending order of their length using a custom comparator
sort(v.begin(), v.end(), cmp);
cout << "\n\nThe elements of the Unordered Set after sorting in descending Order of their length using a custom comparator are: \n";
//declaring an iterator to iterate through the vector
for (it = v.begin(); it != v.end(); it++)
cout << *it << " "; //accessing the elements of the vector using * as i stores the address to each element
cout << "\n\n\n";
We hope that this post helped you develop a better understanding of the concept of writing a Custom Sort method to sort an Unordered Set and its implementation in CPP. For any query, feel free to reach out to us via the comments section down below.
Keep Learning : )