How to sort with a lambda?

BTR picture BTR · Feb 25, 2011 · Viewed 168k times · Source
sort(mMyClassVector.begin(), mMyClassVector.end(), 
    [](const MyClass & a, const MyClass & b)
{ 
    return a.mProperty > b.mProperty; 
});

I'd like to use a lambda function to sort custom classes in place of binding an instance method. However, the code above yields the error:

error C2564: 'const char *' : a function-style conversion to a built-in type can only take one argument

It works fine with boost::bind(&MyApp::myMethod, this, _1, _2).

Answer

BTR picture BTR · Feb 25, 2011

Got it.

sort(mMyClassVector.begin(), mMyClassVector.end(), 
    [](const MyClass & a, const MyClass & b) -> bool
{ 
    return a.mProperty > b.mProperty; 
});

I assumed it'd figure out that the > operator returned a bool (per documentation). But apparently it is not so.