Skip to main content

$Order Function, continued

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

Terminal


USER>write

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

USER>

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

For example, this code puts the next subscript after 4 in a, and puts the value stored there in b.

Terminal


USER>set a = $order(x(4)), b = x(a) write a, "   ", b 
9   3
USER>

You 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).

Terminal


USER>set a = $order(x(4), 1, b) write a, "   ", b
9   3
USER>set a = $order(x(9), -1, b) write a, "   ", b
4   2
USER>

FeedbackOpens in a new tab