Siebel eScript Language Reference > Siebel eScript Commands > Array Objects >

Array sort() Method

The sort() method sorts the elements of an array into the order specified by the compareFunction.





A user-defined function that can affect the sort order


arrayName with its elements sorted into the order specified.


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 allows 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.


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.

    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");
    Clib.fprintf(fp, "After sort: " + a.join() + "\n");

Siebel eScript Language Reference