diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index 2314e325ba..3ea40ccc8a 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -230,13 +230,14 @@ public: } /** Remove a certain object */ - void removeObject(T object) + void removeObject(T object, bool toRelease = true) { CCASSERT(object != nullptr, "The object should not be nullptr"); auto iter = std::find(_data.begin(), _data.end(), object); if (iter != _data.end()) _data.erase(iter); - object->release(); + if (toRelease) + object->release(); } /** Removes an element with a certain index */ @@ -341,6 +342,16 @@ public: }); } + void sort(const std::function& callback) + { + if (empty()) + return; + + std::sort(_data.begin(), _data.end(), [&callback](T a, T b) -> bool{ + return callback(a, b); + }); + } + // ------------------------------------------ // Iterators // ------------------------------------------