KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Sort Collection of Strings in Numeric Order
PRODUCT: 4D | VERSION: 19 R 6 | PLATFORM: Mac & Win
Published On: January 16, 2023

By utilizing .orderByMethod() we are able to sort a collection of strings in numeric order, as opposed to sorting them in alphabetical order.

The example below illustrates .orderByMethod() and it's behavior with formula callback. The results are stored in local variables $c2 and $c3.

var $c1; $c2; $c3 : Collection
$c1:=New collection("123"; "987"; "456"; "23")

$c2:=$c1.orderBy() // ["123","23","456","987"] alphabetical order

$c3:=$c1.orderByMethod(Formula(Num($1.value)<Num($1.value2)))
//["23","123","456","987"]


.orderByMethod() returns a new collection containing all elements of the collection in the order defined through the formula 4D function

Note: This function does not modify the original collection.

This returns a shallow copy, meaning 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.