This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v19.8
collection.sort( )
|
collection.sort ( {methodName {; extraParam}{; extraParam2 ; ... ; extraParamN}} ) -> Function result | ||||||||
Parameter | Type | Description | ||||||
methodName | Text |
![]() |
Name of method used to specify the sorting order | |||||
extraParam | Expression |
![]() |
Parameter(s) for the method | |||||
Function result | Collection |
![]() |
Original collection sorted | |||||
The collection.sort( ) method sorts the elements of the original collection and also returns the sorted collection.
Note: This method modifies the original collection.
If collection.sort( ) is called with no parameters, only scalar values (number, text, date, booleans) are sorted. Elements are sorted by default in ascending order, according to their type.
If you want to sort the collection elements in some other order or sort any type of element, you must supply in methodName 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.
If the collection contains elements of different types, they are first grouped by type and sorted afterwards. Types are returned in the following order:
C_COLLECTION($col)
$col:=New collection("Tom";5;"Mary";3;"Henry";1;"Jane";4;"Artie";6;"Chip";2)
$col2:=$col.sort() // $col2=["Artie","Chip","Henry","Jane","Mary","Tom",1,2,3,4,5,6]
// $col=["Artie","Chip","Henry","Jane","Mary","Tom",1,2,3,4,5,6]
C_COLLECTION($col)
$col:=New collection(10;20)
$col2:=$col.push(5;3;1;4;6;2).sort() //$col2=[1,2,3,4,5,6,10,20]
C_COLLECTION($col)
$col:=New collection(33;4;66;1111;222)
$col2:=$col.sort() //numerical sort: [4,33,66,222,1111]
$col3:=$col.sort("numberOrder") //alphabetical sort: [1111,222,33,4,66]
//numberOrder project method
C_OBJECT($1)
$1.result:=String($1.value)<String($1.value2)
Product: 4D
Theme: Collections
Number:
805259
Created: 4D v16 R6
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)