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
This commit is contained in:
@@ -53,27 +53,27 @@ public class ArrayUtil
|
|||||||
* Perform a stable sort on the specified array.
|
* 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
|
* @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,
|
* 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
|
// insertion sort implementation
|
||||||
|
var nn :int = arr.length;
|
||||||
var n :int = arr.length;
|
for (var ii :int = 1; ii < nn; ii++) {
|
||||||
for (var i :int = 1; i < n; ++i) {
|
var val :* = arr[ii];
|
||||||
var val :* = arr[i];
|
var jj :int = ii - 1;
|
||||||
var j :int = i - 1;
|
for (; jj >= 0; jj--) {
|
||||||
while (j >= 0) {
|
var compVal :* = arr[jj];
|
||||||
var compVal :* = arr[j];
|
|
||||||
if (comp(val, compVal) < 0) {
|
if (comp(val, compVal) < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
arr[jj + 1] = compVal;
|
||||||
arr[j + 1] = compVal;
|
|
||||||
--j;
|
|
||||||
}
|
}
|
||||||
|
arr[jj + 1] = val;
|
||||||
arr[j + 1] = val;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user