Given an ordered struct, is there a simple way to ...
# lucee
l
Given an ordered struct, is there a simple way to insert a new key/value at a specific position? In this case I need to insert the new k/v in the penultimate position. Simple with arrays, but I can't seem to figure out a way to do it other than brute forcing it (i.e. grab the key array, use array functions to insert new key, and then reassemble the ordered struct)
b
@leftbower Ordered structs preserve insertion order
So you'd need to create a new struct in the wanted order
There is also a sorted struct if you want the keys to be alphabetical, etc
At least on Lucee there's one
l
thanks @bdw429s that's as I thought. I have it implemented that way now... just thought I'd see if there was some way to "insert at" or "swap" positions (e.g. some sort of index under the hood that I could manipulate!)
b
Under the hood, Lucee uses a
LinkedHashMap
instance, however • Lucee's
StructImpl
class doesn't actually give you direct access to the wrapped map object • even if it did, a linked hashmap doesn't really have any different methods https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html It just sticks to the
Map
interface and the rest is an implementation detail.