sort vector of strings in terms of lengths of strings

Given a vector<string>words . sort the vector on the basis of length of strings present in the vector. 


#include<bits/stdc++.h>
using namespace std;
bool comparator(string &s1, string &s2)
{
    return s1.length() < s2.length();
}
void print(vector<string>&words)
{
    for(int i =0 ; i< words.size() ; i++)
    {
        cout << words[i] << " " ;
    }
}
int main()
{
   
    vector<string>words = {"a", "sgud", "sjs", "ahjdh", "ahsjkskla"};
    sort(words.begin(), words.end(), comparator);
    print(words);
   
}

Comments