It is easy to sort collections of pointers using STL's sort method. You simply have to implement a comparison function that takes pointer arguments instead of references to object instances. Presumably you already had to do something like this in your own sort function.
I wish that Qt did that though
It does, exactly the way STL's sort does: there are two versions of qSort(), the second of which takes a comparison function as the third argument. So you simply write:
bool myPointerLessThan( MyPointer * p1, MyPointer * p2 )
{
if ( p1 && p2 )
return p1->someValue < p2->someValue;
return false;
}
QList< MyPointer * > myList;
// ... fill the list somehow
qSort( myList.begin(), myList.end(), myPointerLessThan );
bool myPointerLessThan( MyPointer * p1, MyPointer * p2 )
{
if ( p1 && p2 )
return p1->someValue < p2->someValue;
return false;
}
QList< MyPointer * > myList;
// ... fill the list somehow
qSort( myList.begin(), myList.end(), myPointerLessThan );
To copy to clipboard, switch view to plain text mode
Can't get much easier than that.
Bookmarks