I have a C++ std::vector denoted as:
std::vector<GameObject*> vectorToSort;
Each object in vectorToSort contains a float parameter which is returned by calling "DistanceFromCamera()":
vectorToSort.at(position)->DistanceFromCamera();
I wish to sort the vector by this float parameter however std::sort does not appear to be able to do this. How can I achieve this sort?
you want to use a predicate like this:
struct VectorSortP {
bool operator()(const GameObject *a, const GameObject *b) const {
return a->DistanceFromCamera() < b->DistanceFromCamera();
}
};
std::sort(vectorToSort.begin(), vectorToSort.end(), VectorSortP());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With