<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: std::sort() cannot sort a NodeArrayList properly in FlexSim Forum</title>
    <link>https://forums.autodesk.com/t5/flexsim-forum/std-sort-cannot-sort-a-nodearraylist-properly/m-p/13524855#M34952</link>
    <description>&lt;P&gt;I bet the 100 mark is a point where std::sort switches from one sorting algorithm to another, e.g. it may do a simple insertion sort if the number of elements is less than 100, but it switches to some other algorithm when it reaches 100, and the NodeListArray doesn't work with that new algorithm. We'll have to take a look to see how that works (or, you could find the solution yourself, as the entire NodeListArray is defined in the header file for you).&lt;/P&gt;</description>
    <pubDate>Fri, 16 Jun 2017 15:33:53 GMT</pubDate>
    <dc:creator>anthony_johnsonT83CM</dc:creator>
    <dc:date>2017-06-16T15:33:53Z</dc:date>
    <item>
      <title>std::sort() cannot sort a NodeArrayList properly</title>
      <link>https://forums.autodesk.com/t5/flexsim-forum/std-sort-cannot-sort-a-nodearraylist-properly/m-p/13524852#M34949</link>
      <description>&lt;P&gt;&lt;I&gt;[ FlexSim 17.1.1 ]&lt;/I&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;In TestManager's onReset(), I create 100 StdTesters and sort them based on the third value. See the following code:&lt;/P&gt;
&lt;PRE&gt;virtual double onReset() override
	{
		FlexSimObject::onReset();

		_sdtTesters.clear();
		for (int i = 0; i &amp;lt; 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-&amp;gt;getThirdVal() &amp;lt; rhs-&amp;gt;getThirdVal();
		});

		return 0;
	}&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;Source code is attached for reference. &lt;A id="7015" href="https://answers.flexsim.com/storage/attachments/7015-samplecode.zip"&gt;samplecode.zip&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Could you provide suggestions to fix it?&lt;/P&gt;
&lt;P&gt;Thanks, Hao&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2017 17:59:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/flexsim-forum/std-sort-cannot-sort-a-nodearraylist-properly/m-p/13524852#M34949</guid>
      <dc:creator>haozhou88FBM</dc:creator>
      <dc:date>2017-06-09T17:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: std::sort() cannot sort a NodeArrayList properly</title>
      <link>https://forums.autodesk.com/t5/flexsim-forum/std-sort-cannot-sort-a-nodearraylist-properly/m-p/13524853#M34950</link>
      <description>&lt;P&gt;
	Usually, std::sort() works NodeListArrays. As a test to see if it is the NodeListArray or std::sort(), try putting all the object in the node list array in a vector, something like this:&lt;/P&gt;
&lt;PRE&gt;std::vector&amp;lt;SdtTester*&amp;gt; testers;
testers.reserve(_sdtTesters.size());
for (SdtTester* tester : _sdtTesters) {
    testers.push_back(tester);
}
std::sort(testers.begin(), testers.end(), [](SdtTester *lhs, SdtTester *rhs) {
    return lhs-&amp;gt;getThirdVal() &amp;lt; rhs-&amp;gt;getThirdVal();
});
for (int i = 0 ; i &amp;lt; testers.size(); i++) {
    testers&lt;I&gt;-&amp;gt;holder-&amp;gt;rank = i + 1;
}
&lt;/I&gt;&lt;/PRE&gt;&lt;P&gt;
	Let me know how that goes. If it doesn't crash, then we can look in to a bug in the NodeListArray class. If it crashes, we can look in to something else.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 16:50:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/flexsim-forum/std-sort-cannot-sort-a-nodearraylist-properly/m-p/13524853#M34950</guid>
      <dc:creator>JordanLJohnson</dc:creator>
      <dc:date>2017-06-14T16:50:30Z</dc:date>
    </item>
    <item>
      <title>Re: std::sort() cannot sort a NodeArrayList properly</title>
      <link>https://forums.autodesk.com/t5/flexsim-forum/std-sort-cannot-sort-a-nodearraylist-properly/m-p/13524854#M34951</link>
      <description>&lt;P&gt;Hi Jordan,&lt;/P&gt;
&lt;P&gt;Thanks for the reply. According to your suggestion, instead of calling std::sort() directly for NodeListArray, I put all objects into a vector and sort them through that vector, and it shows that sorting works properly with any size vector size. So that means NodeListArray may have problems.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jun 2017 20:24:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/flexsim-forum/std-sort-cannot-sort-a-nodearraylist-properly/m-p/13524854#M34951</guid>
      <dc:creator>haozhou88FBM</dc:creator>
      <dc:date>2017-06-15T20:24:14Z</dc:date>
    </item>
    <item>
      <title>Re: std::sort() cannot sort a NodeArrayList properly</title>
      <link>https://forums.autodesk.com/t5/flexsim-forum/std-sort-cannot-sort-a-nodearraylist-properly/m-p/13524855#M34952</link>
      <description>&lt;P&gt;I bet the 100 mark is a point where std::sort switches from one sorting algorithm to another, e.g. it may do a simple insertion sort if the number of elements is less than 100, but it switches to some other algorithm when it reaches 100, and the NodeListArray doesn't work with that new algorithm. We'll have to take a look to see how that works (or, you could find the solution yourself, as the entire NodeListArray is defined in the header file for you).&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 15:33:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/flexsim-forum/std-sort-cannot-sort-a-nodearraylist-properly/m-p/13524855#M34952</guid>
      <dc:creator>anthony_johnsonT83CM</dc:creator>
      <dc:date>2017-06-16T15:33:53Z</dc:date>
    </item>
    <item>
      <title>Re: std::sort() cannot sort a NodeArrayList properly</title>
      <link>https://forums.autodesk.com/t5/flexsim-forum/std-sort-cannot-sort-a-nodearraylist-properly/m-p/13524856#M34953</link>
      <description>&lt;P&gt;Thank you Anthony!&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 18:38:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/flexsim-forum/std-sort-cannot-sort-a-nodearraylist-properly/m-p/13524856#M34953</guid>
      <dc:creator>haozhou88FBM</dc:creator>
      <dc:date>2017-06-16T18:38:25Z</dc:date>
    </item>
  </channel>
</rss>

