Sort Array Method
The Sort Array method sorts array elements into an order that you specify. It returns the sorted array elements.
Format
arrayName.sort([compareFunction])
The following table describes the arguments for the Sort Array method.
Argument | Description |
---|---|
compareFunction |
Specifies the sort order. This method does the sort differently depending on if you include the compareFunction argument:
|
Example
The following example uses the Sort Array method with and without a compare function:
function compareNumbers(a, b)
{
return a - b;
}
var a = new Array(5, 3, 2, 512);
var fp = Clib.fopen("C:\\log\\Trace.log", "a");
Clib.fprintf(fp, "Before sort: " + a.join() + "\n");
a.sort(compareNumbers);
Clib.fprintf(fp, "After sort: " + a.join() + "\n");
Clib.fclose(fp);
This example does the following:
Displays the results of a sort without the function.
Uses the following function to sort the numbers:
compareNumbers(a, b)
In this function, if a and b are two array elements that Siebel eScript compares, then Siebel eScript does the following:
If compareNumbers(a, b) is less than zero, then it gives b a lower index than a.
If compareNumbers(a, b) returns zero, then it does not modify the order of a and b.
If compareNumbers(a, b) is greater than zero, then it gives b a higher index than a.