Siebel eScript Language Reference > Methods Reference > Array Methods >

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])

Table 34 describes the arguments for the Sort Array method.

Table 34. 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:

  • Include the compareFunction argument. It sorts array elements according to the return value that the compare function contains.
  • Do not include the compareFunction argument. It converts array elements to strings before it sorts them. It sorts numbers in ASCII order, comparing them from left to right. For example, 32 comes before 4. The compareFunction argument allows you to modify this sort behavior.
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:

  1. Displays the results of a sort without the function.
  2. 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.
Siebel eScript Language Reference Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices.