You can pass a function pointer, function object (or boost lambda) to std::sort to define a strict weak ordering of the elements of the container you want sorted.
Howeve
You can try this:
Usage:
struct Citizen {
std::wstring iFirstName;
std::wstring iLastName;
};
ChainComparer cmp;
cmp.Chain( boost::bind( &Citizen::iLastName, _1 ) );
cmp.Chain( boost::bind( &Citizen::iFirstName, _1 ) );
std::vector vec;
std::sort( vec.begin(), vec.end(), cmp );
Implementation:
template
class ChainComparer {
public:
typedef boost::function TComparator;
typedef TComparator EqualComparator;
typedef TComparator CustomComparator;
template class TComparer, typename TValueGetter>
void Chain( const TValueGetter& getter ) {
iComparers.push_back( std::make_pair(
boost::bind( getter, _1 ) == boost::bind( getter, _2 ),
boost::bind( TComparer(), boost::bind( getter, _1 ), boost::bind( getter, _2 ) )
) );
}
bool operator()( const T& lhs, const T& rhs ) {
BOOST_FOREACH( const auto& comparer, iComparers ) {
if( !comparer.first( lhs, rhs ) ) {
return comparer.second( lhs, rhs );
}
}
return false;
}
private:
std::vector> iComparers;
};