I recently noticed that std::sort() cannot sort a NodeArrayList properly if it is larger than a certain size. To replicate the problem, I create a class called SdtTester that inherits from SDT. It is a simple class that only binds three doubles. I also create a class called TestManager that inherits from FlexSimObject. A variable is created in the TestManager class to hold a bunch of SdtTesters.
In TestManager's onReset(), I create 100 StdTesters and sort them based on the third value. See the following code:
virtual double onReset() override { FlexSimObject::onReset(); _sdtTesters.clear(); for (int i = 0; i < 100; i++) { _sdtTesters.add(new SdtTester(uniform(-50, 50), uniform(-50, 50), uniform(-50, 50))); } std::sort(_sdtTesters.begin(), _sdtTesters.end(), [](SdtTester *lhs, SdtTester *rhs) { return lhs->getThirdVal() < rhs->getThirdVal(); }); return 0; }
This problem is FlexSim will crash by the std::sort() function. But if I change the list size to be 99 instead of 100, FlexSim will create and sort all the element properly. This is very confused.
Another problem is that if I still create 100 elements, but I sort them based on the first value instead of the third value. FlexSim will work properly.
Source code is attached for reference. samplecode.zip
Could you provide suggestions to fix it?
Thanks, Hao