Next Previous Contents

17. array_sort


Sort an array


Array_Type array_sort (Array_Type a, String_Type f)


array_sort sorts the array a into ascending order according to the function specified by the name f and returns an integer array that represents the result of the sort.

The sort function represented by f must be a S-lang user-defined function that takes two arguments. The function must return an integer that is less than zero if the first parameter is considered to be less than the second, zero if they are equal, and a value greater than zero if the first is greater than the second.

The integer array returned by this function is simply an index that indicates the order of the sorted array. The input array a is not changed.


An array of strings may be sorted using the strcmp function since it fits the specification for the sorting function described above:

     variable A = String_Type [3];
     A[0] = "gamma"; A[1] = "alpha"; A[2] = "beta";

     variable I = array_sort (A, "strcmp");
After the array_sort has executed, the variable I will have the values [2, 0, 1]. This array can be used to re-shuffle the elements of A into the sorted order via the array index expression A = A[I].

The current sorting algorithm is a heap-sort.

See Also


Next Previous Contents