Added a sort() method that operates on arrays of Comparable elements.
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4273 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -5,6 +5,25 @@ package com.threerings.util {
|
|||||||
*/
|
*/
|
||||||
public class ArrayUtil
|
public class ArrayUtil
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Sort the specified array according to natural order- all elements
|
||||||
|
* must implement Comparable.
|
||||||
|
*/
|
||||||
|
public static function sort (arr :Array) :void
|
||||||
|
{
|
||||||
|
// ensure every item is Comparable
|
||||||
|
if (!arr.every(function (item :*, index :int, array :Array) :Boolean
|
||||||
|
{
|
||||||
|
return (item is Comparable);
|
||||||
|
})) {
|
||||||
|
throw new Error("Not all elements are Comparable instances.");
|
||||||
|
}
|
||||||
|
|
||||||
|
arr.sort(function (obj1 :Object, obj2 :Object) :int {
|
||||||
|
return (obj1 as Comparable).compareTo(obj2);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the first instance of the specified element from the array.
|
* Remove the first instance of the specified element from the array.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user