From d70b867ddb3a13bf9dcd2bd97d9bfaec5d552c55 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Thu, 20 Dec 2007 21:21:44 +0000 Subject: [PATCH] Make null keys work with SortedHashMap. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4914 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/ArrayUtil.as | 12 ++++++++++-- src/as/com/threerings/util/SortedHashMap.as | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/as/com/threerings/util/ArrayUtil.as b/src/as/com/threerings/util/ArrayUtil.as index 8dfa5f029..af840f122 100644 --- a/src/as/com/threerings/util/ArrayUtil.as +++ b/src/as/com/threerings/util/ArrayUtil.as @@ -32,12 +32,20 @@ public class ArrayUtil { /** * Sort the specified array according to natural order- all elements - * must implement Comparable. + * must implement Comparable or be null. */ public static function sort (arr :Array) :void { arr.sort(function (obj1 :Object, obj2 :Object) :int { - return Comparable(obj1).compareTo(obj2); + if (obj1 == obj2) { // same object or both null + return 0; + } else if (obj1 == null) { + return -1; + } else if (obj2 == null) { + return 1; + } else { + return Comparable(obj1).compareTo(obj2); + } }); } diff --git a/src/as/com/threerings/util/SortedHashMap.as b/src/as/com/threerings/util/SortedHashMap.as index 0edf7bd34..4e41f5eeb 100644 --- a/src/as/com/threerings/util/SortedHashMap.as +++ b/src/as/com/threerings/util/SortedHashMap.as @@ -88,6 +88,9 @@ public class SortedHashMap extends HashMap protected function validateKey (key :Object) :void // throws ArgumentError { + if (key == null) { + return; + } switch (_keyType) { case COMPARABLE_KEYS: if (key is Comparable) {