Contains sorting Comparators.
These functions take any parameters but require only the first to, so they are suitable
for passing to Array.sort(), or with a flex Sort object. Note that returning values other
than 1, 0, or -1 confuse a flex Sort object.
In general, these functions don't handle sorting undefined, it will be coerced to null.
public static function compareBooleans(v1:Boolean, v2:Boolean, ... ignored):int
Compares two Boolean values.
Parameters
| v1:Boolean |
|
| v2:Boolean |
|
| ... ignored |
Returns
public static function compareComparables(c1:Comparable, c2:Comparable, ... ignored):int
A standard Comparator for comparing Comparable values.
Parameters
Returns
public static function compareEnumsByName(e1:Enum, e2:Enum, ... ignored):int
Compare two Enums by their name().
If your Enum doesn't override toString(), then you could just use compareStrings.
Parameters
Returns
public static function compareInts(v1:int, v2:int, ... ignored):int
Compares two int values in an overflow safe manner.
Parameters
| v1:int |
|
| v2:int |
|
| ... ignored |
Returns
public static function compareNumbers(v1:Number, v2:Number, ... ignored):int
Compares two Number values, taking into account the intricacies of dealing with NaN.
Parameters
| v1:Number |
|
| v2:Number |
|
| ... ignored |
Returns
public static function compareStrings(s1:String, s2:String, ... ignored):int
Compare two objects by their toString() values, case sensitively.
Yes, you can pass any objects to this function, and actionscript will coerce them
to Strings, calling toString() if not a simple type.
Parameters
| s1:String |
|
| s2:String |
|
| ... ignored |
Returns
public static function compareStringsInsensitively(s1:String, s2:String, ... ignored):int
Compare two objects by their toString().toLowerCase() values, case insensitively.
Yes, you can pass any objects to this function, and actionscript will coerce them
to Strings, calling toString() if not a simple type.
Parameters
| s1:String |
|
| s2:String |
|
| ... ignored |
Returns
public static function compareUnknowns(o1:Object, o2:Object, ... ignored):int
Compare two values whose type is not known at compile type. Tries to figure it out.
Also handles nulls.
Note that this method may look like it can safely compare heterogeneous types,
but it cannot. Consider the array: [ true, "meerkat", false ].
compareUnknowns(true, "meerkat") returns -1;
compareUnknowns("meerkat", false) returns -1;
but compareUnknowns(true, false) returns 1, which violates transitivity.
If in doubt, try using createNullSafe(compareStrings), which can safely and consistently
sort anything.
Parameters
| o1:Object |
|
| o2:Object |
|
| ... ignored |
Returns
public static function createFields(sortFields:Array, defaults:Array = null):Function
Create a Comparator function that sorts according to one or more fields of Objects.
Array.sortOn() only works with public variables, and not with public getters. This
implementation works with both.
Parameters
| sortFields:Array |
|
| defaults:Array (default = null )
|
Returns
public static function createFor(clazz:Class):Function
Create a Comparator appropriate for comparing objects of the specified class.
If an appropriate comparator cannot be determined, compareUnknowns is returned.
Parameters
Returns
public static function createNullSafe(comparator:Function):Function
Compose another comparator into one that can compare null and non-null elements
safely, sorting the nulls to the bottom.
Parameters
Returns
Example
Arrays.sort(myStringArray, Comparators.createNullSafe(Comparators.compareStrings));
public static function createReverse(comparator:Function):Function
Create a Comparator function that reverses the ordering of the specified Comparator.
Parameters
Returns
Copyright © 2007-2009 Three Rings Design, Inc.