This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v19.8
collection.orderByMethod( )

collection.orderByMethod( ) 


 

collection.orderByMethod ( methodName {; extraParam}{; extraParam2 ; ... ; extraParamN} ) -> Function result 
Parameter Type   Description
methodName  Text in Name of method used to specify the sorting order
extraParam  Expression in Parameter(s) for the method
Function result  Collection in Sorted copy of the collection (shallow copy)

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))

You want to sort a collection by character code or language:

 var $strings1;$strings2 : Collection
 $strings1:=New collection("Alpha";"Charlie";"alpha";"bravo";"Bravo";"charlie")
 
  //using the character code
 $strings2:=$strings1.orderByMethod("sortCollection";sk character codes)
  // result: ["Alpha","Bravo","Charlie","alpha","bravo","charlie"]
 
  //using the language
 $strings2:=$string1s.orderByMethod("sortCollection";sk strict)
  // result: ["alpha","Alpha","bravo","Bravo","charlie","Charlie"]

 

The sortCollection method:

 var$1Object
 var$2Integer // sort option
 
 $1.result:=(Compare strings($1.value;$1.value2;$2)<0)



See also 

collection.orderBy( )
collection.sort( )

 
PROPERTIES 

Product: 4D
Theme: Collections
Number: 705253

This command can be run in preemptive processes

 
PAGE CONTENTS 
 
HISTORY 

Created: 4D v16 R6

 
ARTICLE USAGE

4D Language Reference ( 4D v19)
4D Language Reference ( 4D v19.1)
4D Language Reference ( 4D v19.4)
4D Language Reference ( 4D v19.5)
4D Language Reference ( 4D v19.6)
4D Language Reference ( 4D v19.7)
4D Language Reference ( 4D v19.8)