Ionic Storage seems to sort keys alphabetical

Hi there,
Recently I am working on Ionic application which would be hopefully deployed to mobile platforms. Basically Im storing some data in Ionic storage like this:‘nickname’,“username”)
.then(() => {‘level’,1)})
.then(() => {‘xp’, 0)})
.then(() => {‘dex’, 1)})
.then(() => {‘speed’, 1)})
.then(() => {‘end’, 1)})
.then(() => {‘str’, 1)})

what I believe is standard approach, just keys and values.

Later when I’ve got the storage setted up like this, I would like to get data out of storage… and here comes the strange thing atleast for me. When I try to get all data out of storage using forEach method of the storage itself and log them into console, the data seems to be sorted alphabetical by keys…

To be clear:, key, index) =>
console.log("Index: ", index);
console.log("Key: ", key);

This code results in log:

Index: 1
Key: dex
Index: 2
Key: end
Index: 3
Key: level
Index: 4
Key: nickname
Index: 5
Key: quest
Index: 6
Key: speed
Index: 7
Key: str
Index: 8
Key: xp
Index: 9

I would expect Key ‘nickname’ to be indexed as 1, because it was inserted first according to the code on top of this post. I havent found any note about storage sorting items, maybe Im missing something, i dont know.
In the end, my question is… why this “sort” happens and is reliable that the data would be always sort like this if I’ll be iterating over them like this and picking up certain indexes every time on every device (for example, if I would like to get data from indexes 1-5, can I be sure, that I will, using this approach, find there keys: dex, end, level, nickname, quest?

Please excuse me, if I’m missing something important.

Thanks in advance.

I’d say your storage method is a bit weird, not that you can’t do it this way, I just think it would be nicer to store one object with all those properties. What I mean is

const player= {
  nickname: 'nickname',
  level: 1,
  xp: 0
};'user', player);

I just don’t know why you’d want to have to do repeated calls every time you want to save or retrieve data, just get it all at once.

As far as accessing, if you need to access things in order, you should probably use an array, object properties don’t have a guaranteed order. But again I don’t know why you’d want to access these by index, if I wanted to get a players dexterity rating I’d do player.dex, not player[3].

If for some reason though you do want an ordered list of properties, you probably want like:

const player = {
  properties: [
    { name: 'dex', value: 1 },
    { name: 'end', value 1 }

This order would be maintained, so you could do[0] and always get the dex object, which would have the name and value. Having them be objects with names is optional, you could literally do ['nickname', 0, 1, 1, 1], but…that seems the most horrible out of any of these possibilities.


Oh… yes, of course… I didnt think about storing whole object, for some reason I didnt come to this solution… from the beggining I was like “lets store every attribute separately”.

And for accessing, with this approach I wouldnt need to access items by index. I wanted to acces all of these attributes at once using forEach instead of getting every attribute with separate storage.get(‘key’)… but, your advice already solved that. I’ll just get whole object out of storage…

Feel ashamed, that this didnt come to my mind and I had to pollute this forum with unneccessary question.

Thanks a lot for your reply, it’s valuable for my problem.