May 08 2002

I think I found a bug in Flash MX (at least the

Published by at 4:57 under Wayback Archive

I think I found a bug in Flash MX (at least the player I currently use in my authoring environment). I was playing around with the new ActionScript feature array.sortOn(“fieldname”) and found out that it does not work if the field you are sorting on contains numbers. Even more specific: it happens when the biggest value in that field counts more numbers than the smallest value, or when the values are written like an exponential value (2e-12). It looks like Flash threaths the values as strings instead of real values.

This is a script I’ve written to test this bug:

var _array = new Array();

for (var i=0; i<11; i++){_array.push({name: "item_"+i, value:i})}
traceArrayValues("Before");
_array.sortOn("value");
traceArrayValues("After ");
function traceArrayValues(string){
var str = string + " ";
for (var j=0; j<_array.length; j++){str+=_array[j].value+" ";}
trace(str);
}

When played in the Flash MX authoring environment you’ll get the following log:

Before 0 1 2 3 4 5 6 7 8 9 10

After 0 1 10 2 3 4 5 6 7 8 9

Of course this is wrong, as 10 is bigger than 2. Before and after should have been the same! But don’t worry, there’s a solution. Simply overwrite the Array.sortOn() method with a new prototype which can be found at [PROTO]type.

[update]

Branden Hall replied the following to a topic I posted at flashcoders:

It’s not a bug, it’s a FOL (Fact of Life). Doing what we refer to as a “natural sort” isn’t very common in programs (hell even Windows still sorts the way you describe, but there is an extension to make Macs sort in a natural way)

The good news is that you can in fact just use plain old sort and use a custom sort method to do what you want. Just keep in mind that it will be much slower than the built in sort.

Thanks Branden!

[update 2]

The complete topic at flashcoders has been archived.

No responses yet

Comments are closed.