<?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: Getting the Most Out of Array.sort()</title>
	<link>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort</link>
	<description>Luck is the residue of good design.</description>
	<pubDate>Thu, 09 Sep 2010 11:11:42 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>

	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-147236</link>
		<pubDate>Thu, 13 Mar 2008 01:54:50 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-147236</guid>
					<description>julz_hk,

I'm afraid I may not have fully understood your question.  I &lt;em&gt;think&lt;/em&gt; you're asking how to use your &lt;code&gt;randomsort()&lt;/code&gt; function when it's declared as the method of a custom class.  If so, you need to create an instance of this class, then reference the class's method.  If this class is named &lt;code&gt;CustomClass&lt;/code&gt;, for example, you would do this:

&lt;pre&gt;&lt;code&gt;var cc:CustomClass = new CustomClass();
this_array.sort(cc.randomsort);&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>julz_hk,</p>
<p>I&#8217;m afraid I may not have fully understood your question.  I <em>think</em> you&#8217;re asking how to use your <code>randomsort()</code> function when it&#8217;s declared as the method of a custom class.  If so, you need to create an instance of this class, then reference the class&#8217;s method.  If this class is named <code>CustomClass</code>, for example, you would do this:</p>
<pre><code>var cc:CustomClass = new CustomClass();
this_array.sort(cc.randomsort);</code></pre>
]]></content:encoded>
				</item>
	<item>
		<title>by: julz_hk</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-146084</link>
		<pubDate>Tue, 11 Mar 2008 08:58:12 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-146084</guid>
					<description>Hmm - I'm using a randomizer as the sort function - ie an easy array scrambler function. My confusion is when using array sort in a class: ie
in the class constructor method :

	this_array.sort(randomsort);

and later in the class:

function randomrange(minval, maxval) { 
return minval + Math.floor(Math.random() * (maxval + 1 - minval));	}

function randomsort() { 
//pass a function to sort makes it the sort criteria.. in this case just shuffle..
var t = randomrange(-1, 1);
return t
}</description>
		<content:encoded><![CDATA[<p>Hmm - I&#8217;m using a randomizer as the sort function - ie an easy array scrambler function. My confusion is when using array sort in a class: ie<br />
in the class constructor method :</p>
<p>	this_array.sort(randomsort);</p>
<p>and later in the class:</p>
<p>function randomrange(minval, maxval) {<br />
return minval + Math.floor(Math.random() * (maxval + 1 - minval));	}</p>
<p>function randomsort() {<br />
//pass a function to sort makes it the sort criteria.. in this case just shuffle..<br />
var t = randomrange(-1, 1);<br />
return t<br />
}
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-43771</link>
		<pubDate>Wed, 09 May 2007 02:01:28 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-43771</guid>
					<description>Marcos,

That sounds like you're looping through the array until equal to its length rather than &lt;em&gt;less than&lt;/em&gt; its length.</description>
		<content:encoded><![CDATA[<p>Marcos,</p>
<p>That sounds like you&#8217;re looping through the array until equal to its length rather than <em>less than</em> its length.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Marcos Paim</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-35690</link>
		<pubDate>Thu, 19 Apr 2007 21:59:27 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-35690</guid>
					<description>David,

I've a problem with the randomize move from label to label . My need is the same that the Brian (february 10th) but with 49 elements in the Array. &quot;My&quot; function is equal to that your reply to Brian's question, except by the number of elements (frame labels).

Everything in function works perfectly, until in 25th execution of getRandomLabel. In that moment the functions trace show in the list a &quot;undefined&quot; result. The trace of  labels.length  correctly shows the amount to it of elements of the Array.

Can you help me with some possible solution for my  problem?
Your blog has been helpful in my learning. 

Thank you very much.

Marcos Paim</description>
		<content:encoded><![CDATA[<p>David,</p>
<p>I&#8217;ve a problem with the randomize move from label to label . My need is the same that the Brian (february 10th) but with 49 elements in the Array. &#8220;My&#8221; function is equal to that your reply to Brian&#8217;s question, except by the number of elements (frame labels).</p>
<p>Everything in function works perfectly, until in 25th execution of getRandomLabel. In that moment the functions trace show in the list a &#8220;undefined&#8221; result. The trace of  labels.length  correctly shows the amount to it of elements of the Array.</p>
<p>Can you help me with some possible solution for my  problem?<br />
Your blog has been helpful in my learning. </p>
<p>Thank you very much.</p>
<p>Marcos Paim
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: NSurveyor</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-119</link>
		<pubDate>Tue, 16 May 2006 23:20:54 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-119</guid>
					<description>Before I knew how it worked, I was amazed how the sort allowed you to give the options seperated by &amp;#124;. It's rather simple. All the options are actually numbers - specifically, powers of 2. Array.CASEINSENSITIVE  is 1. Array.DESCENDING  is 2. Array.UNIQUESORT is 4. Array.RETURNINDEXEDARRAY is actually 8. Array.NUMERIC is 16. In binary, they would be: 00001, 00010, 00100, 01000, 10000. expression1 &amp;#124;  expression2 will convert &quot;expression1 and expression2 to 32-bit unsigned integers, and returns a 1 in each bit position where the corresponding bits of either expression1 or expression2 are 1.&quot; So.... 00001 &amp;#124; 00100 would give 00101 (or 5). Given this number, Flash can determine the options to use.</description>
		<content:encoded><![CDATA[<p>Before I knew how it worked, I was amazed how the sort allowed you to give the options seperated by |. It&#8217;s rather simple. All the options are actually numbers - specifically, powers of 2. Array.CASEINSENSITIVE  is 1. Array.DESCENDING  is 2. Array.UNIQUESORT is 4. Array.RETURNINDEXEDARRAY is actually 8. Array.NUMERIC is 16. In binary, they would be: 00001, 00010, 00100, 01000, 10000. expression1 |  expression2 will convert &#8220;expression1 and expression2 to 32-bit unsigned integers, and returns a 1 in each bit position where the corresponding bits of either expression1 or expression2 are 1.&#8221; So&#8230;. 00001 | 00100 would give 00101 (or 5). Given this number, Flash can determine the options to use.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-66</link>
		<pubDate>Sun, 30 Apr 2006 19:11:53 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-66</guid>
					<description>Glad to hear it, Tom!  :)</description>
		<content:encoded><![CDATA[<p>Glad to hear it, Tom!  <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Tom Button</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-65</link>
		<pubDate>Sun, 30 Apr 2006 14:13:26 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-65</guid>
					<description>Thanks for the example of how to define the list as numeric when you sort it - this has solved a problem for me where I had 10</description>
		<content:encoded><![CDATA[<p>Thanks for the example of how to define the list as numeric when you sort it - this has solved a problem for me where I had 10
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-38</link>
		<pubDate>Mon, 17 Apr 2006 16:56:23 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-38</guid>
					<description>NSurveyor,

Thanks for turning that one up again!  I knew that was out there, but hadn't been able to find it in a while.</description>
		<content:encoded><![CDATA[<p>NSurveyor,</p>
<p>Thanks for turning that one up again!  I knew that was out there, but hadn&#8217;t been able to find it in a while.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: NSurveyor</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-32</link>
		<pubDate>Mon, 17 Apr 2006 16:01:04 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-32</guid>
					<description>It's pretty inefficient though. 

Here's a nice little discussion on different sort methods people came up with, and the best of one of all as well:

&lt;a href=&quot;http://groups.google.com/group/macromedia.flash.actionscript/browse_frm/thread/831835576870f132?tvc=1&amp;#38;hl=en&quot; target=&quot;
_blank&quot;&gt;http://groups.google.com/group/macromedia.flash.actionscript/&lt;/a&gt;&lt;br /&gt;&lt;a href=&quot;http://groups.google.com/group/macromedia.flash.actionscript/browse_frm/thread/831835576870f132?tvc=1&amp;#38;hl=en&quot; target=&quot;
_blank&quot;&gt;browse_frm/thread/831835576870f132?tvc=1&amp;#38;hl=en&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>It&#8217;s pretty inefficient though. </p>
<p>Here&#8217;s a nice little discussion on different sort methods people came up with, and the best of one of all as well:</p>
<p><a href="http://groups.google.com/group/macromedia.flash.actionscript/browse_frm/thread/831835576870f132?tvc=1&amp;hl=en" target="<br />
_blank"><a href='http://groups.google.com/group/macromedia.flash.actionscript/' rel='nofollow'>http://groups.google.com/group/macromedia.flash.actionscript/</a></a><br /><a href="http://groups.google.com/group/macromedia.flash.actionscript/browse_frm/thread/831835576870f132?tvc=1&amp;hl=en" target="<br />
_blank">browse_frm/thread/831835576870f132?tvc=1&amp;hl=en</a>
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-19</link>
		<pubDate>Wed, 12 Apr 2006 13:47:13 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/getting-the-most-array-sort#comment-19</guid>
					<description>木頭,

Your example makes use of the custom sorting algorithm described in the article.  In your case, you're supplying the parameter as a function literal, which is perfectly fine.  The function itself relies on two factors:  the global &lt;code&gt;random()&lt;/code&gt; function, which returns a pseudo-random integer between zero and one less than the number given, and the conditional operator.

&lt;pre&gt;&lt;code&gt;random(2);
// returns either 0 or 1
random(3);
// returns 0, 1, or 2
// etc.&lt;/code&gt;&lt;/pre&gt;

The ActionScript Language Reference recommends the &lt;code&gt;Math.random()&lt;/code&gt; method over the &lt;code&gt;random()&lt;/code&gt; function, which was deprecated in Flash 5.  &lt;code&gt;Math.random()&lt;/code&gt; is a bit different in that it accepts no parameters and returns a pseudo-random float greater than or equal to zero and less than one.  You would need to use &lt;code&gt;Math.round()&lt;/code&gt; in addition to &lt;code&gt;Math.random()&lt;/code&gt; to achieve the same effect as &lt;code&gt;random()&lt;/code&gt; alone.

&lt;pre&gt;&lt;code&gt;Math.round(Math.random());&lt;/code&gt;&lt;/pre&gt;

The conditional operator works like this ...

(expression) ? result1 : result2

... in which an expression is evaluated as true or false.  If &lt;code&gt;true&lt;/code&gt;, the first result is returned; if &lt;code&gt;false&lt;/code&gt;, the second is returned.

In your case, the expression is &lt;code&gt;random(2)&lt;/code&gt;, which I'll update to the newer version ...

&lt;pre&gt;&lt;code&gt;(Math.round(Math.random())) ? 1: -1;&lt;/code&gt;&lt;/pre&gt;

Remember, the number 1 represents &lt;code&gt;true&lt;/code&gt; and 0 represents &lt;code&gt;false&lt;/code&gt;, so that provides what the conditional operator needs.  If the expression evaluates to &lt;code&gt;true&lt;/code&gt;, the number 1 will be returned, which means element A will be positioned ahead of element B.  If the expression evaluates to &lt;code&gt;false&lt;/code&gt;, the number -1 will be returned, which means the opposite.

So rather than an expression like &lt;code&gt;a &gt; b&lt;/code&gt;, which is entirely predictable (element A is either greather than element B or it isn't), you're providing a random algorithm, which effectively &quot;shuffles&quot; your array.</description>
		<content:encoded><![CDATA[<p>木頭,</p>
<p>Your example makes use of the custom sorting algorithm described in the article.  In your case, you&#8217;re supplying the parameter as a function literal, which is perfectly fine.  The function itself relies on two factors:  the global <code>random()</code> function, which returns a pseudo-random integer between zero and one less than the number given, and the conditional operator.</p>
<pre><code>random(2);
// returns either 0 or 1
random(3);
// returns 0, 1, or 2
// etc.</code></pre>
<p>The ActionScript Language Reference recommends the <code>Math.random()</code> method over the <code>random()</code> function, which was deprecated in Flash 5.  <code>Math.random()</code> is a bit different in that it accepts no parameters and returns a pseudo-random float greater than or equal to zero and less than one.  You would need to use <code>Math.round()</code> in addition to <code>Math.random()</code> to achieve the same effect as <code>random()</code> alone.</p>
<pre><code>Math.round(Math.random());</code></pre>
<p>The conditional operator works like this &#8230;</p>
<p>(expression) ? result1 : result2</p>
<p>&#8230; in which an expression is evaluated as true or false.  If <code>true</code>, the first result is returned; if <code>false</code>, the second is returned.</p>
<p>In your case, the expression is <code>random(2)</code>, which I&#8217;ll update to the newer version &#8230;</p>
<pre><code>(Math.round(Math.random())) ? 1: -1;</code></pre>
<p>Remember, the number 1 represents <code>true</code> and 0 represents <code>false</code>, so that provides what the conditional operator needs.  If the expression evaluates to <code>true</code>, the number 1 will be returned, which means element A will be positioned ahead of element B.  If the expression evaluates to <code>false</code>, the number -1 will be returned, which means the opposite.</p>
<p>So rather than an expression like <code>a > b</code>, which is entirely predictable (element A is either greather than element B or it isn&#8217;t), you&#8217;re providing a random algorithm, which effectively &#8220;shuffles&#8221; your array.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
