About Chandra Archive Proposer Instruments & Calibration Newsletters Data Analysis HelpDesk Calibration Database NASA Archives & Centers Chandra Science Links

Skip the navigation links
Last modified: December 2006

URL: http://cxc.harvard.edu/ciao3.4/array_sort.tm.html
Hardcopy (PDF): A4 | Letter
AHELP for CIAO 3.4 array_sort Context: slangrtl

Synopsis

Sort an array

Syntax

Array_Type array_sort (Array_Type a [, String_Type or Ref_Type f])

Description

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.

Example

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

Hardcopy (PDF): A4 | Letter
Last modified: December 2006



The Chandra X-Ray Center (CXC) is operated for NASA by the Smithsonian Astrophysical Observatory.
60 Garden Street, Cambridge, MA 02138 USA.    Email: cxcweb@head.cfa.harvard.edu
Smithsonian Institution, Copyright © 1998-2004. All rights reserved.