As others have mentioned, you could use a comparison function, but you can also overload the < operator and the default less<T>
functor will work as well:
struct data {
string word;
int number;
bool operator < (const data& rhs) const {
return word.size() < rhs.word.size();
}
};
Then it's just:
std::sort(info.begin(), info.end());
Edit
As James McNellis pointed out, sort
does not actually use the less<T>
functor by default. However, the rest of the statement that the less<T>
functor will work as well is still correct, which means that if you wanted to put struct data
s into a std::map
or std::set
this would still work, but the other answers which provide a comparison function would need additional code to work with either.