

































<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Troubleshooting Tales:&#160; The Case of the Mysterious Array.sortOn() Call</title>
	<link>http://www.quip.net/blog/2007/flash/actionscript-20/troubleshooting-tales-case-array-sorton</link>
	<description>Luck is the residue of good design.</description>
	<pubDate>Wed, 08 Feb 2012 15:29:23 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>

	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2007/flash/actionscript-20/troubleshooting-tales-case-array-sorton#comment-13452</link>
		<pubDate>Sat, 20 Jan 2007 23:55:24 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/actionscript-20/troubleshooting-tales-case-array-sorton#comment-13452</guid>
					<description>NSurveyor,

&lt;blockquote&gt;wouldn’t your addCuePoint throw a mismatch error as your cuePointTime is strictly data typed as a Number?&lt;/blockquote&gt;

Now that you mention it, yes, it sure &lt;em&gt;should&lt;/em&gt; &amp;#8212; but it doesn't.  Isn't that odd?  I imagine it would in AS3, in which strong typing truly is strongly typed.

&lt;blockquote&gt;Also, I’m thinking sorting after adding adding each element isn’t very efficient, because every time you add an element, you are resorting the ENTIRE list.&lt;/blockquote&gt;

Heh, yes, that's definitely true.  In this case, I decided &amp;#8212; perhaps poorly &amp;#8212; to sweep that point under the rug in deference to the main theme, especially as this was a migration piece.  Sometimes details can distract the from an article's flow.  Finding that balance can be a challenge. 

Your point is well taken, as always, and your code samples are great!  :)  Thanks, bro!</description>
		<content:encoded><![CDATA[<p>NSurveyor,</p>
<blockquote><p>wouldn’t your addCuePoint throw a mismatch error as your cuePointTime is strictly data typed as a Number?</p></blockquote>
<p>Now that you mention it, yes, it sure <em>should</em> &mdash; but it doesn&#8217;t.  Isn&#8217;t that odd?  I imagine it would in AS3, in which strong typing truly is strongly typed.</p>
<blockquote><p>Also, I’m thinking sorting after adding adding each element isn’t very efficient, because every time you add an element, you are resorting the ENTIRE list.</p></blockquote>
<p>Heh, yes, that&#8217;s definitely true.  In this case, I decided &mdash; perhaps poorly &mdash; to sweep that point under the rug in deference to the main theme, especially as this was a migration piece.  Sometimes details can distract the from an article&#8217;s flow.  Finding that balance can be a challenge. </p>
<p>Your point is well taken, as always, and your code samples are great!  <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   Thanks, bro!
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: NSurveyor</title>
		<link>http://www.quip.net/blog/2007/flash/actionscript-20/troubleshooting-tales-case-array-sorton#comment-13405</link>
		<pubDate>Sat, 20 Jan 2007 17:56:47 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/actionscript-20/troubleshooting-tales-case-array-sorton#comment-13405</guid>
					<description>Immediately after reading that the &quot;numbers&quot; were sorting incorrectly, I thought, &quot;Oh, he must be using Strings!&quot; :)

Though I am a bit curious... wouldn't your addCuePoint throw a mismatch error as your cuePointTime is strictly data typed as a Number?

Also, I'm thinking sorting after adding adding each element isn't very efficient, because every time you add an element, you are resorting the ENTIRE list. So, I would perhaps either have a method that

1. adds all the cue points, and then sorts. 
2. adds each cue point in the correct position by adding it before the first element that is greater than or equal to it (finding this element by looping through the array).

As a case study, I have timed the amount of time it takes for these 3 methods to sort (the same) list of 1000 elements, where each element is randomly chosen in the range 0-49.

I find that Method 1 (sorting afterwards) is faster in all cases. 

Method 2 (manually finding where element belongs) is USUALLY faster than the original method, for arrays of length greater than 200, but slower for less than that. However, this algorithm could probably be made more efficient.

And for one very exaggerated test of having 1000 cue points, the original method took 14509 ms (!), method 1 took 13 ms, and method 2 took 6324 ms.

Here's the code that I used:

//Random array
var r = [];
for(var i=0;i&amp;#60;1000;i++){
	r[i] = random(50);
}
//For each element, add and sort
var s = getTimer();
var a = [];
for(var i=0;i&amp;#60;r.length;i++){
	a[i] = r[i];
	a.sort(Array.NUMERIC);
}
trace(getTimer()-s);
//Add all elements, then sort
var s = getTimer();
var b = [];
for(var i=0;i&amp;#60;r.length;i++){
	b[i] = r[i];
}
b.sort(Array.NUMERIC);
trace(getTimer()-s);
//For each element, add in correct position
var s = getTimer();
var c = [];
for(var i=0;i&amp;#60;r.length;i++){
	var addE = r[i];
	for(var j=0;j&amp;#60;c.length;j++){
		if(c[j]&amp;#62;=addE){
			break;
		}
	}
	c.splice(j,0,addE);
}
trace(getTimer()-s);</description>
		<content:encoded><![CDATA[<p>Immediately after reading that the &#8220;numbers&#8221; were sorting incorrectly, I thought, &#8220;Oh, he must be using Strings!&#8221; <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Though I am a bit curious&#8230; wouldn&#8217;t your addCuePoint throw a mismatch error as your cuePointTime is strictly data typed as a Number?</p>
<p>Also, I&#8217;m thinking sorting after adding adding each element isn&#8217;t very efficient, because every time you add an element, you are resorting the ENTIRE list. So, I would perhaps either have a method that</p>
<p>1. adds all the cue points, and then sorts.<br />
2. adds each cue point in the correct position by adding it before the first element that is greater than or equal to it (finding this element by looping through the array).</p>
<p>As a case study, I have timed the amount of time it takes for these 3 methods to sort (the same) list of 1000 elements, where each element is randomly chosen in the range 0-49.</p>
<p>I find that Method 1 (sorting afterwards) is faster in all cases. </p>
<p>Method 2 (manually finding where element belongs) is USUALLY faster than the original method, for arrays of length greater than 200, but slower for less than that. However, this algorithm could probably be made more efficient.</p>
<p>And for one very exaggerated test of having 1000 cue points, the original method took 14509 ms (!), method 1 took 13 ms, and method 2 took 6324 ms.</p>
<p>Here&#8217;s the code that I used:</p>
<p>//Random array<br />
var r = [];<br />
for(var i=0;i&lt;1000;i++){<br />
	r[i] = random(50);<br />
}<br />
//For each element, add and sort<br />
var s = getTimer();<br />
var a = [];<br />
for(var i=0;i&lt;r.length;i++){<br />
	a[i] = r[i];<br />
	a.sort(Array.NUMERIC);<br />
}<br />
trace(getTimer()-s);<br />
//Add all elements, then sort<br />
var s = getTimer();<br />
var b = [];<br />
for(var i=0;i&lt;r.length;i++){<br />
	b[i] = r[i];<br />
}<br />
b.sort(Array.NUMERIC);<br />
trace(getTimer()-s);<br />
//For each element, add in correct position<br />
var s = getTimer();<br />
var c = [];<br />
for(var i=0;i&lt;r.length;i++){<br />
	var addE = r[i];<br />
	for(var j=0;j&lt;c.length;j++){<br />
		if(c[j]&gt;=addE){<br />
			break;<br />
		}<br />
	}<br />
	c.splice(j,0,addE);<br />
}<br />
trace(getTimer()-s);
</p>
]]></content:encoded>
				</item>
</channel>
</rss>

