Array_Type array_sort (Array_Type a [, String_Type or Ref_Type f])
array_sort sorts the array a into ascending order and
returns an integer array that represents the result of the sort. If
the optional second parameter f is present, the function
specified by f will be used to compare elements of a;
otherwise, a built-in sorting function will be used.
If f is present, then it must be either a string representing
the name of the comparison function, or a reference to the function.
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.
If the comparision function is not specified, then a built-in comparison
function appropriate for the data type will be used. For example,
if a is an array of character strings, then the sort will be
preformed using strcmp.
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);
Alternatively, one may use
variable I = array_sort (A);
to use the built-in comparison function.
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].
|