4D v17.4

collection.orderByMethod( )

Home

 
4D v17.4
collection.orderByMethod( )

collection.orderByMethod( )  


 

The collection.orderByMethod( ) method returns a new collection containing all elements of the collection in the order defined through the methodName method.

This method returns a shallow copy, which means that objects or collections in both collections share the same reference. If the original collection is a shared collection, the returned collection is also a shared collection.

Note: This method does not modify the original collection.

In methodName, pass a comparison method that compares two values and returns true in $1.result if the first value is lower than the second value. You can provide additional parameters to methodName if necessary.

  • methodName will receive the following parameters:
    • $1 (object), where:
      • $1.value (any type): first element value to be compared
      • $1.value2 (any type): second element value to be compared
    • $2...$N (any type): extra parameters
  • methodName sets the following parameter:
    • $1.result (boolean): true if $1.value < $1.value2, false otherwise

You want to sort a collection of strings in numerical order rather than alphabetical order:

 C_COLLECTION($c)
 $c:=New collection
 $c.push("33";"4";"1111";"222")
 $c2:=$c.orderBy() //$c2=["1111","222","33","4"], alphabetical order
 $c3:=$c.orderByMethod("NumAscending") // $c3=["4","33","222","1111"]

Here is the code for NumAscending:

 $1.result:=Num($1.value)<Num($1.value2)

You want to sort a collection of strings on their length:

 C_COLLECTION($fruits)
 $fruits:=New collection("Orange";"Apple";"Grape";"pear";"Banana";"fig";"Blackberry";"Passion fruit")
 $c2:=$fruits.orderByMethod("WordLength")
  //$c2=[Passion fruit,Blackberry,Orange,Banana,Apple,Grape,pear,fig]

Here is the code for WordLength:

 $1.result:=Length(String($1.value))>Length(String($1.value2))



See also 

collection.orderBy( )
collection.sort( )

 
PROPERTIES 

Product: 4D
Theme: Collections

This command can be run in preemptive processes

 
PAGE CONTENTS 
 
HISTORY 

 
ARTICLE USAGE

4D Language Reference ( 4D v17)
4D Language Reference ( 4D v17.1)
4D Language Reference ( 4D v17.2)
4D Language Reference ( 4D v17.3)
4D Language Reference ( 4D v17.4)