3 ms·
What I'm talking about is arbitrary attributes that might not exist on the base class, resulting in something like: std::sort( myvec.begin(), myvec.
by nrdvana 9y ago
What I'm talking about is arbitrary attributes that might not exist on the base class, resulting in something like:
std::sort(
myvec.begin(),
myvec.end(),
[](obj& A, obj& b) {
int A_attr1= dynamic_cast<Thing1*>(&A)? dynamic_cast<Thing1*>(&A)->attr1
: dynamic_cast<Thing2*>(&A)? dynamic_cast<Thing2*>(&A)->attr1
: -1;
int B_attr1= dynamic_cast<Thing1*>(&B)? dynamic_cast<Thing1*>(&B)->attr1
: dynamic_cast<Thing2*>(&B)? dynamic_cast<Thing2*>(&B)->attr1
: -1;
return A_attr1 < B_attr1;
}
);
and then compare that to
@list= sort { ($a->{attr1} // -1) <=> ($b->{attr1} // -1) } @list;