$Order Function
You finished Part 2 by creating an index, ^PersonI, where most of the data is stored in the subscripts. Now it's time for you to learn how to access data stored in subscripts. Given a node in an array, the $Order function gives you access to the values of the subscripts of its children. Because subscripts are automatically sorted, this function traverses the children in order, hence its name.
To use it, you pass $Order an array name along with one or more of its subscripts. The array and all but the final, rightmost subscript identify a node. The final subscript is the subscript on which $Order focuses. In the example using the x array, the subscript on which $Order focuses is the first and only subscript. Remember that the empty string ("") is not allowed to be a subscript of an array. But it can be used as the subscript of the array passed to $Order. It causes $Order to return the first valid subscript.
So $Order returns the first valid subscript, in this case, 1. Passing 1 as the subscript in turn returns the next valid subscript, 4. Passing 4 returns 9, and passing 9 causes $Order to return the empty string, signifying that there aren't any more valid subscripts.
If you supply a subscript value that doesn't exist in the array; $Order returns the next valid subscript that follows that value.
USER>write
x(1)=1
x(4)=2
x(9)=3
USER>write $order(x(""))
1
USER>write $order(x(1))
4
USER>write $order(x(4))
9
USER>write $order(x(9))
USER>write $order(x(2.5))
4
USER>write $order(x(8))
9
USER>