B4J Question Map sorting

uniplan

Active Member
Licensed User
Longtime User
I created a map by entering the individual elements by ordering the key in alphabetical order.
I then saved the map to a file with the File.WriteMap statement.
I then reloaded the map with the File.ReadMap statement.
Finally I read the first element of the map with the instruction:

B4X:
myMap.GetKeyAt (0)

The element I found was not the first one I had inserted.

Is there a way to reload the map in the same order I did the insertion?

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
In almost all cases it is a mistake to use Map.GetKeyAt or GetValueAt.
You should access the map based on the keys or with a For Each loop.

If the order is important then you should probably switch to a List with custom types. You can serialize it with B4XSerializator.

You can preserve the map order with File.ReadMap2 or by serializing it with B4XSerializator.
 
Upvote 0
Top