Skip to main content

$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>

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

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

FeedbackOpens in a new tab