From 9ce195b99d1f9eec622e02599cd45051c9ed35a4 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Tue, 8 Apr 2008 19:37:00 +0000 Subject: [PATCH] Made the Comparator optional. Match the coding style already present in file (use ii for loop counter..) git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4991 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/ArrayUtil.as | 28 ++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/as/com/threerings/util/ArrayUtil.as b/src/as/com/threerings/util/ArrayUtil.as index e74de5441..c3b658b50 100644 --- a/src/as/com/threerings/util/ArrayUtil.as +++ b/src/as/com/threerings/util/ArrayUtil.as @@ -53,27 +53,27 @@ public class ArrayUtil * Perform a stable sort on the specified array. * @param comp a function that takes two objects in the array and returns -1 if the first * object should appear before the second in the container, 1 if it should appear after, - * and 0 if the order does not matter. + * and 0 if the order does not matter. If omitted, Comparators.COMPARABLE is used and + * the array elements should be Comparable objects. */ - public static function stableSort (arr :Array, comp :Function) :void + public static function stableSort (arr :Array, comp :Function = null) :void { + if (comp == null) { + comp = Comparators.COMPARABLE; + } // insertion sort implementation - - var n :int = arr.length; - for (var i :int = 1; i < n; ++i) { - var val :* = arr[i]; - var j :int = i - 1; - while (j >= 0) { - var compVal :* = arr[j]; + var nn :int = arr.length; + for (var ii :int = 1; ii < nn; ii++) { + var val :* = arr[ii]; + var jj :int = ii - 1; + for (; jj >= 0; jj--) { + var compVal :* = arr[jj]; if (comp(val, compVal) < 0) { break; } - - arr[j + 1] = compVal; - --j; + arr[jj + 1] = compVal; } - - arr[j + 1] = val; + arr[jj + 1] = val; } }