The SORT function returns a vector of subscripts that allow access to the elements of Array in ascending order.


Example 1

A = [4, 3, 7, 1, 2]
; Display the elements of A in sorted order:
PRINT, 'Elements of A in sorted order: ', A[SORT(A)]
; Display the elements of A in descending order:
PRINT, 'Elements of A in descending order: ', A[REVERSE(SORT(A))]

IDL prints:

SORT(A) =   3   4   1   0   2
Elements of A in sorted order:   1   2   3   4   7
Elements of A in descending order:   7   4   3   2   1

SORT(A) returns “3 4 1 0 2” because:

A[3] < A[4] < A[1] < A[0] < A[2]

Example 2

Sorting NaN Values

When sorting data including Not A Number (NaN) values, the NaN entries are moved to the end of the resulting array. For example:

values = [ 500, !VALUES.F_NAN, -500 ]
PRINT, SORT(values)

IDL prints:

2           0           1


Result = SORT(Array [, /L64] )

Return Value

The result is always a vector of integer type with the same number of elements as Array.

Note: If Array contains any identical elements, the order in which the identical elements are sorted is arbitrary and may vary between operating systems.



The array to be sorted. Array can be any basic type of vector or array. String arrays are sorted using the ASCII collating sequence. Complex arrays are sorted by their magnitude. Array values which are Not A Number (NaN) are moved to the end of the resulting array.



By default, the result of SORT is 32-bit integer when possible, and 64-bit integer if the number of elements being sorted requires it. Set L64 to force 64-bit integers to be returned in all cases.

Note: Only 64-bit versions of IDL are capable of creating variables requiring a 64-bit sort. Check the value of !VERSION.MEMORY_BITS to see if your IDL is 64-bit or not.

Version History



See Also