Reputation: 115
I'm trying to write a sort function for vectors using lambda, and I've seen this suggested at so many places, but I've always had problems doing this.
std::sort(eventvec.begin(), eventvec.end(), [](const event& lhs, const event& rhs) {
return lhs.getim < rhs.getim;});
I had multiple errors while writing it and then I kind of stacked it on a function within a function as it needed a static function.
The functions declared in the class are:
double event::getut() { return unift; }
static double getim(event &a) { return a.getut(); }
In the end the sorting is not in order at all. There are negative values as well in the attribute. Any suggestions?
Upvotes: 0
Views: 111
Reputation: 62719
You are sorting your events by the address of event::getim
, which is the same for every event.
I think you mean to call a member function of event, which needs to be callable from a const event
double event::getut() const { return unift; }
std::sort(eventvec.begin(), eventvec.end(), [](const event& lhs, const event& rhs) {
return lhs.getut() < rhs.getut(); });
Upvotes: 3