In this C++ tutorial, you shall learn how to sort a string vector based on the length of strings, with example programs.

Sort string Vector based on string length in C++

To sort elements in a string vector, based on the string length of elements, in ascending order, in C++, use std::sort() function of algorithm library.

std::sort() function of algorithm library can sort the vector in ascending order, based on the comparing function that we provide as argument. The comparing function must take two arguments (elements) for comparing, and must return a boolean value.

The code for comparing function, and sort() method, to sort the elements of string vector v in ascending order based on the element’s string length is

bool cmp(const string lhs, const string rhs) {
   return lhs.length() < rhs.length();
}

sort(v.begin(), v.end(), cmp);

For example, when the strings "apple" and "fig" are compared based on length, "fig" is smaller with a length of 3 than that of "apple" with a length of 5.

To sort the string vector, based on length, in descending order, reverse the sorted() vector which is already sorted in ascending order, using vector reverse().

sort(v.begin(), v.end());
reverse(v.begin(), v.end());

Or, we can rewrite the comparison function as shown below where we check if lhs > rhs.

bool cmp(const string lhs, const string rhs) {
   return lhs.length() > rhs.length();
}

C++ Programs

ADVERTISEMENT

1. Sort strings in vector in ascending order of their length

In the following program, we take a string vector in v and sort this vector in ascending order based on the string length of elements.

main.cpp

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(const string lhs, const string rhs) {
   return lhs.length() < rhs.length();
}

int main() {
   //string vector
   vector<string> v { "banana", "apple", "avocado", "fig" };

   //sort string vector based on string length
   sort(v.begin(), v.end(), cmp);
   
   //print result vector
   for ( auto& element : v ) {
      cout << element << "  ";
   }
}

Output

fig  apple  banana  avocado

2. Sort strings in vector in descending order of their length

In the following program, we sort the string vector v in descending order based on the string length of elements.

main.cpp

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(const string lhs, const string rhs) {
   return lhs.length() > rhs.length();
}

int main() {
   //string vector
   vector<string> v { "banana", "apple", "avocado", "fig" };

   //sort string vector based on string length
   sort(v.begin(), v.end(), cmp);
   
   //print result vector
   for ( auto& element : v ) {
      cout << element << "  ";
   }
}

Output

avocado  banana  apple  fig

You can also try by not changing the comparison function but using reverse() function.

Conclusion

In this C++ Tutorial, we learned how to sort given string vector in ascending order or descending order based on the string length.