DisplayUtil.sortDisplayChildren now uses ArrayUtil.stableSort

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@460 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Tom Conkling
2008-04-08 19:19:43 +00:00
parent 81c5760863
commit 17829f4ae5
+20 -4
View File
@@ -21,6 +21,7 @@
package com.threerings.flash { package com.threerings.flash {
import com.threerings.util.ArrayUtil;
import com.threerings.util.ClassUtil; import com.threerings.util.ClassUtil;
import flash.display.DisplayObject; import flash.display.DisplayObject;
@@ -31,16 +32,31 @@ import flash.geom.Rectangle;
public class DisplayUtil public class DisplayUtil
{ {
/** /**
* Quicksorts container's children. * Sorts a container's children, using ArrayUtil.stableSort.
* *
* comp is a function that takes two DisplayObjects, and returns int -1 if the first * comp is a function that takes two DisplayObjects, and returns int -1 if the first
* object should appear before the second in the container, 1 if it should appear after, * object should appear before the second in the container, 1 if it should appear after,
* and 0 if the order does not matter. (Since quicksort is not a stable sort algorithm, * and 0 if the order does not matter.
* returning 0 in the compare function is equivalent to returning -1).
*/ */
public static function sortDisplayChildren (container :DisplayObjectContainer, comp :Function) :void public static function sortDisplayChildren (container :DisplayObjectContainer, comp :Function) :void
{ {
qsortDisplayChildren(container, 0, container.numChildren - 1, comp); var numChildren :int = container.numChildren;
var children :Array = new Array(numChildren);
// pull the display children into an array.
// guess that removing children from the end of a DisplayObjectContainer
// is more efficient than removing them from the beginning.
for (var i :int = numChildren - 1; i >= 0; --i) {
children[i] = container.removeChildAt(i);
}
// stable sort the array
ArrayUtil.stableSort(children, comp);
// add children back to the container
for each (var child :DisplayObject in children) {
container.addChild(child);
}
} }
/** /**