Skip to main content

This is documentation for Caché & Ensemble.

For information on converting to InterSystems IRISOpens in a new window, see the InterSystems IRIS Migration Guide and Migrating to InterSystems IRIS, both available on the WRC Distributions pageOpens in a new window (login required).

$Order Function, continued

$Order can also traverse subscripts in reverse order, using -1 for the second argument.

SAMPLES>write

x(1)=1
x(4)=2
x(9)=3
SAMPLES>write $order( x(""), -1 )
9
SAMPLES>write $order( x(9), -1 )
4
SAMPLES>write $order( x(4), -1 )
1
SAMPLES>write $order( x(1), -1 )

SAMPLES>
Copy code to clipboard

Often, when using $Order to get valid subscripts, you also want to retrieve the value stored in that location in the array.

For example, we get the next subscript (a), and the value stored there (b).

SAMPLES>set a = $order( x(4) ), b = x(a) write a, "   ", b 
9   3
Copy code to clipboard

We can do the same thing in a simpler and faster way. If you supply a variable name as the third argument of $Order, you can do this in one step instead of two. The second argument can be 1 (forwards traversal) or -1 (backwards traversal).

SAMPLES>set a = $order( x(4), 1, b) write a, "   ", b
9   3
Copy code to clipboard
FeedbackOpens in a new window