I have a vector<data> info
where data
is defined as:
struct data{
string word;
int number;
};
I need to sort info
by the length of the word strings. Is there a quick and simple way to do it?
Use a comparison function:
bool compareByLength(const data &a, const data &b)
{
return a.word.size() < b.word.size();
}
and then use std::sort
in the header #include <algorithm>
:
std::sort(info.begin(), info.end(), compareByLength);