Event Sort
/**
* Copyright bkzen ( http://wonderfl.net/user/bkzen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/3hvo
*/
// forked from saharan's Sleep sort
package {
import flash.events.*;
import flash.utils.*;
import flash.text.TextField;
import flash.display.Sprite;
public class EventSort extends Sprite {
private var _trace:TextField;
public function EventSort() {
initialize();
}
private function initialize():void {
_trace = new TextField();
_trace.wordWrap = true;
_trace.width = 465;
_trace.height = 465;
addChild(_trace);
var numbers:Array = [];
var i:int;
for (i = 0; i < 200; i++) {
numbers[i] = i;
var flip:int = Math.random() * i;
var temp:uint = numbers[i];
numbers[i] = numbers[flip];
numbers[flip] = temp;
}
sort(numbers);
}
private function sort(numbers:Array):void {
for (var i:int = 0; i < numbers.length; i++) {
var number:uint = numbers[i] as uint;
addEventListener("sort", getHandler(number), false, -number);
}
dispatchEvent(new Event("sort"));
}
private function getHandler(number: uint): Function
{
return function(e: Event): void
{
_trace.appendText(number + " ");
}
}
}
}