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:
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")
Here is the code for WordLength:
$1.result:=Length(String($1.value))>Length(String($1.value2))