From 0387014af8d559256267e0e08361af0c31d8ecd7 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Tue, 18 Jul 2006 02:22:24 +0000 Subject: [PATCH] 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 --- src/as/com/threerings/util/ArrayUtil.as | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/as/com/threerings/util/ArrayUtil.as b/src/as/com/threerings/util/ArrayUtil.as index 8dd50dc0f..096c587ac 100644 --- a/src/as/com/threerings/util/ArrayUtil.as +++ b/src/as/com/threerings/util/ArrayUtil.as @@ -5,6 +5,25 @@ package com.threerings.util { */ 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. */