this question has answer here:
- sort based on multiple things in c++ 5 answers
so basically, i'm trying sort vector of struct entry (each entry has string word , int count) values of ints. managed via inline lambda expression:
vector<entry*> entries(old); //make copy of old vector std::stable_sort(entries.begin(), entries.end(), [] (const entry *lhs, const entry *rhs){ return (lhs->count > rhs->count); }); however, problem have is, if 2 or more entrys have same count, need sort in alphabetical order. possible use lambda expression somewhere in there, or there way this? thanks!
the solution pretty simple:
std::stable_sort(entries.begin(), entries.end(), [] (const entry *lhs, const entry *rhs) { if( lhs->count != rhs->count ) return lhs->count > rhs->count; else return lhs->word > rhs->word; });
Comments
Post a Comment