Sort in Decending Order using Lambda + std::sort ...

Hello,

So I have the following code snippet; what I want to do is sort the std::vector using a custom sort in descending order:

1
2
3
4
5
std::sort(CurrentObjects.begin(), CurrentObjects.end(),
	[](PhysicsObject * a, PhysicsObject * b) -> bool
{
	return a->GetHighestOpacity() < b->GetHighestOpacity();
});


In this case, the "PhysicsObjects" have a an opacity property to them. I would like the sorting algorithm to set up the vector such that the highest opacity (1.0) is in the front of the vector and the lowest is at the rear.

Am I doing this correctly?

Thank you.
This looks OK, so long as CurrentObjects is a container of PhysicObject*s that are never nullptrs.
> I would like the sorting algorithm to set up the vector such that
> the highest opacity (1.0) is in the front of the vector and the lowest is at the rear.

1
2
3
4
5
6
7
8
9
10
11
12
std::sort(CurrentObjects.begin(), CurrentObjects.end(),
	[](PhysicsObject * a, PhysicsObject * b) -> bool
{
	// return a->GetHighestOpacity() < b->GetHighestOpacity();
        return a->GetHighestOpacity() > b->GetHighestOpacity();
        
        /*        robust version
        const double aa = a ? a->GetHighestOpacity() : std::numeric_limits<double>::min() ;
        const double bb = b ? b->GetHighestOpacity() : std::numeric_limits<double>::min() ;
        return std::isnan(bb) ? !std::isnan(aa) : aa > bb ; 
        */
}
Topic archived. No new replies allowed.