KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Collection Member Function orderByMethod
PRODUCT: 4D | VERSION: 16 R6 | PLATFORM: Mac & Win
Published On: May 14, 2018

New in v16 R6 are a whole host of member functions for collections. One of these member functions, orderByMethod, can be used for ordering of collection values based on a custom method.

There are a few things to know about how this member function works:
1. A method needs to exist to handle the sorting, with inputs as $1.value, $1.value2, $2...$N, $1.result
2. $1.value represents the current value in the collection for the comparison
3. $1.value2 represents the next value in the collection for the comparison
4. $2 ... $N represent optional parameters to pass into the custom method
5. $1.result is a boolean and if True the orderByMethod member function will swap the first and second values, and if False will not

To understand how this works, two basic examples can be compared.

Example 1 (comparing two values):

Consider a method, wordLength which is defined as:

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

This wordLength method will compare the length of two values in the collection, and if the first is larger, it will swap position of the two values in the colection. This is a good time to remind that this orderByMethod is a shallow copy, meaning it does not modify the original collection.

See the below image for an example use of this method.



Example 2 (moving a certain value to the begining of the collection):

Consider a method, bubbleUp, which is defined as:

$1.result:=String($1.value)=$2

This bubbleUp method will compare a collection value to the input parameter of the function and if it matches, it will sort it up the collection.

See the below image for an example use of this method.