Home > Back-end >  Why is 100 in front of 11 in firebase documents order?
Why is 100 in front of 11 in firebase documents order?

Time:10-14

I'm building an app that will display a list of user info, with each one a numeric ID that is stored when creating the profile and used as the docID in firebase. To facilitate the search, I used this method so the ID would be displayed in ascending order. But, after some tests, I notice that, for example, 100 comes first then 11. Why does it happen? Is there a way to correct/prevent it?

I was storing ID as String. But, as the answer suggested, I changed it to both int and double. Still, 11 comes after 100

enter image description here

CodePudding user response:

It's hard to say for certain without seeing your database, but most likely you're storing the numbers as strings. In such cases Firebase (both Firestore and Realtime Database) will sort the values lexicographically, and in that order "100" comes before "11" - just as "baa" comes before "bb".

CodePudding user response:

Why is 100 in front of 11 in firebase documents order?

Because you're sorting alphabetically instead of numerically. 0 comes before 1 in most character encodings like ASCII and Unicode.

CodePudding user response:

After the answers I got and some research I finally got it.

The main problem was that I was storing the ID as String.

After fixing it, I added .orderBy('id') in the place where I retrieve the uses' list.

Thank you everyone one for the time spent trying to help me.

  • Related