Packagesystem.comparators
Classpublic class GenericComparator
InheritanceGenericComparator Inheritance Object
Implements Comparator, Sortable

This comparator compare two Objects by field.

Example :

     import system.comparators.GenericComparator ;
     import system.comparators.StringComparator ;
     
     var write:Function = function( ar:Array ):void
     {
         var result:Array = [] ;
         var size:uint = ar.length ;
         for (var i:uint = 0 ; i<size ; i++)
         {
             result[i] = ar[i].label ;
         }
         trace( result.join('|') ) ;
     }
     
     var ar:Array =
     [
         { id : 0 , label : "Paris"     } ,
         { id : 1 , label : "Marseille" } ,
         { id : 2 , label : "Lyon"      } ,
         { id : 3 , label : "Bordeaux"  }
     ] ;
     
     var c:GenericComparator = new GenericComparator( "label", StringComparator.getStringComparator() ) ;
     
     ar.sort( c.compare , Array.CASEINSENSITIVE ) ;
     write(ar) ; // Lyon|Paris|Bordeaux|Marseille
     
     ar.sort( c.compare , Array.CASEINSENSITIVE | Array.DESCENDING ) ;
     write(ar) ; // Paris|Marseille|Lyon|Bordeaux
     



Public Properties
 PropertyDefined By
  comparator : Comparator
Determinates the Comparator strategy used to sort the instance.
GenericComparator
  sortBy : String
Determinates a String who represents a property name to compare the two objects.
GenericComparator
Public Methods
 MethodDefined By
  
GenericComparator(sortBy:String, comparator:Comparator)
Creates a new GenericComparator instance.
GenericComparator
  
compare(o1:*, o2:*, options:* = null):int
Returns an integer value to compare two objects.
GenericComparator
Property Detail
comparatorproperty
comparator:Comparator

Determinates the Comparator strategy used to sort the instance.

The internal Comparator used by this Comparator to compare two objects with the specified field.


Implementation
    public function get comparator():Comparator
    public function set comparator(value:Comparator):void

Throws
ReferenceError — If the 'comparator' property is 'null'.
sortByproperty 
sortBy:String

Determinates a String who represents a property name to compare the two objects.


Implementation
    public function get sortBy():String
    public function set sortBy(value:String):void

Throws
ArgumentError — If the 'sortBy' property is 'null'.
Constructor Detail
GenericComparator()Constructor
public function GenericComparator(sortBy:String, comparator:Comparator)

Creates a new GenericComparator instance.

Parameters
sortBy:String — A String who represents a property name to compare the two objects.
 
comparator:Comparator — An Comparator use to compare the two objects with the specified property names.

Throws
ArgumentError — If the 'sortBy' argument is 'null'.
 
ArgumentError — If the 'comparator' argument is 'null'.
Method Detail
compare()method
public function compare(o1:*, o2:*, options:* = null):int

Returns an integer value to compare two objects.

Parameters

o1:* — the first Number object to compare.
 
o2:* — the second Number object to compare.
 
options:* (default = null)

Returns
int

  • -1 if o1 is "lower" than (less than, before, etc.) o2 ;
  • 1 if o1 is "higher" than (greater than, after, etc.) o2 ;
  • 0 if o1 and o2 are equal.

  • Throws
    ArgumentError — if the two arguments are null.