Answer the question
In order to leave comments, you need to log in
How to write a comparator for std::find?
Good evening. I read here ( https://en.cppreference.com/w/cpp/algorithm/sort ) how to write a comparator for std::find, but I catch 3 errors. You need to sort objects like this:
What's wrong?std::pair<std::string, unsigned int>.
std::sort(std::begin(not_sorted_result), std::end(not_sorted_result), [](const std::pair<std::string, unsigned int >& first, const std::pair<std::string, unsigned int >& second) {
// if size bigger or less sort by length
if (first.first.size() > second.first.size())
return true;
// if size is the same sort by alphabet
else if (first.first.size() == second.first.size())
if (first.first[0] > second.first[0])
return true;
else
return false;
else
return false;
});
Answer the question
In order to leave comments, you need to log in
If you are satisfied with the fact that if the strings match exactly, sorting by numbers will occur - you don’t need to write anything, std::pair has a standard comparator that works lexicographically.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question