Ordered Dictionary

Does anyone have any code for a custom Dictionary class that preserves the order of items added? I know that the native Xojo Dictionary does not guarantee the order but I am porting some Java code that uses Java’s LinkedHashSet which is essentially an ordered Dictionary.

I know that Monkeybread has a class that does this but this is for an open source project that can’t rely on third party license-based plugins so please don’t recommend that.

Suggestion: subclass a regular Dictionary and override the appropriate methods to maintain and use an array of keys.

But no, I don’t have that code. Should be easy to whip up though.

Wait, I just saw that the Key( index ) function is available. Doesn’t that give you order?

The problem is the order will change the moment you remove an item. Other than that I did not notify order change, even if the documentation says the order is stable until the dictionary gets changed.

From the documentation about Dictionary.Keys()
The order is stable and matches the order returned by the Values method at least until the Dictionary is modified.

I just tested and I could not get the order to change using Key( index ) (not Keys). We should confirm.

I got clarification. Order is not guaranteed for that function as the Dictionary values change, so you will need something like the subclass I mentioned.

I whipped something up if you want it @Garry Pettet .

Wow Kem! That’s efficient. Only just got round to seeing the responses. I’d very much like to see what you’ve got thanks.

Post or DM your email and I’ll send it to you.

or post it on github etc if its meant to be public ?
or save as text and post the code in the forums ?

When I have more time.

send it to me and I can post it on mine if you want ?

however java’s class is a little different than just a xojo dictionary with ordering
unlike xojo’s the java class does NOT replace the entry if you try to add it a second time
it leaves it alone

Task masters.


grumpy cat ?

It’s true, I too am an adorable worldwide phenomenon.

a legend in your own mind huh ?