Bookshelf Home | Contents | Index | Search | PDF |
Siebel eScript Language Reference > Siebel eScript Commands > Array Objects >
sort() Method
The sort() method sorts the elements of an array into the order specified by the compareFunction.
Syntax
arrayName.sort([compareFunction])
Returns
arrayName with its elements sorted into the order specified.
Usage
If no compareFunction is supplied, then elements are converted to strings before sorting. When numbers are sorted into ASCII order, they are compared left-to-right, so that, for example, 32 comes before 4. This may not be the result you want. However, the compareFunction enables you to specify a different way to sort the array elements. The name of the function you want to use to compare values is passed as the only parameter to sort().
If a compare function is supplied, the array elements are sorted according to the return value of the compare function.
Example
The following example demonstrates the use of the sort() method with and without a compare function. It first displays the results of a sort without the function and then uses a user-defined function,
compareNumbers(a, b)
, to sort the numbers properly. In this function, if a and b are two elements being compared, then:
- If
compareNumbers(a, b)
is less than zero, b is given a lower index than a.- If
compareNumbers(a, b)
returns zero, the order of a and b is unchanged.- If
compareNumbers(a, b)
is greater than zero, b is given a higher index
than a.
Bookshelf Home | Contents | Index | Search | PDF |
Siebel eScript Language Reference Published: 18 April 2003 |