PDA

View Full Version : array.sort


kentyler
04-16-2003, 06:02 AM
It looks like array.sort is one of the things that does not work.

a = new Array(1, 2, 3);
debug.write( a.sort());

throws
Error: array_sort.lzx:7:63: Encountered "sort". Was expecting one of: instanceof, (, [, ,, ., =, >, <, ?, ==, <=, >=, !=, ===, !==, ||, &&, ++, --, +, -, *, /, &, |, ^, %, <<, >>, >>>, +=, -=, *=, /=, &=, |=, ^=, %=, <<=, >>=, >>>=, :

hqm
04-16-2003, 06:31 AM
That's not a runtime error, that's a parser/compiler error, you must have a syntax error someplace else in your file.

This works:


<canvas width="1024" height="768" debug="true">

<script>
a = new Array(5,4,1, 2, 3);
debug.write( a.sort());
</script>
</canvas>



Or, in the debugger, you could verify that by typing

_root.a = new Array(3,2,1)
a.sort()

and you'll se it works.

However, sort is actually implemented by Macromedia's Flash VM, and it in fact does have some bugs. Here is an alternate implementation of sort, using the quicksort algorithm, which is faster than Macromedia's (even though it's written in Javascript!), and which is not buggy:


<![CDATA[

Array.prototype.quicksort_1 = function (a, l,r) {
var i=l, j=r, w, x=a[Math.floor((l+r)/2)];
do {
while(a[i] < x) i++;
while(x < a[j]) j--;
if(i<=j){
w = a[i];
a[i++] = a[j];
a[j--] = w;
}
} while(i <= j);
if(l < j) this.quicksort_1(a, l,j);
if(i < r) this.quicksort_1(a, i, r);
}

Array.prototype.quicksort = function () {
this.quicksort_1(this, 0, this.length-1);
}

]]>

antun
04-16-2003, 01:07 PM
I think so too. I just tried:


<canvas debug="true">
<script>
a = new Array(3, 1, 2);
debug.write( a.sort());
</script>
</canvas>


And it returned 1,2,3

Originally posted by hqm
That's not a runtime error, that's a parser/compiler error, you must have a syntax error someplace else in your file.