Solved - Storing map data in state - can it be sorted?

Does anybody know . . .
Is it possible to store a map with numeric keys into state and maintain sort order?

The store operation is conceptually trivial.
state.storedData = (my sorted map)

When I try to store a sorted map, it seems to lose order. I'm assuming that state may be stored as a hashmap so it doesn't retain order -- is that correct? Or is there some way of keeping order (or iterating in order?)

You can use a list (array) instead if you want them ordered. Like you said, a map really isn't about ordering the entries but rather accessing them with keys like a hash.

What is the use case? You could implement some additional logic using map.keySet(). More info here: Working with collections

For anyone interested in the solution, let's say I have a Map, myData, with integer keys stored in state as state.myData. These will be unsorted - I suspect that state stores information using HashMaps which can't be sorted.

If I wanted to do something to that data in sorted order, first put the keys in an array, sort that, then iterate over that with an "each", as shown

state.myData // Original, un-sorted array
List<Integer> dataKeys = state.myData?.keySet().collect{it as Integer}
def sortedKeys = dataKeys.sort()
sortedKeys.each{ state.myData["${it}"]  // do something with this. Each iteration will be the next key in sorted order }

Note the odd behavior where I have to treat the state.myData key as a string using ["${it}"] rather than as an integer using [${it as Integer}]. It seems, from what I can tell, that if a map with Integer keys is stored in state, the Integer keys get converted to string keys. Anybody know why?