How to order the nodes in firebase console based on key

I have a node for users with key 1 to 1400 but it's not in order. Hot to view this in order in the console. I had a similar experience with another node. But it later got ordered automatically my console


Solution 1:

Unfortunately, you cannot change the order of the nodes in the Firebase Database Console. By default, all the nodes are ordered by key. One thing to remember is that Firebase keys are Strings. And when strings are order, are ordered lexicographically.

So for numbers, this is the normal order:

  • 1308
  • 1309
  • 1310
  • 1311

But for strings, this is the normal order:

  • "1308"
  • "1309"
  • "131"
  • "1310"

There is no operator in Firebase and as far as i know nor in most other databases that allow you to change this behavior. Instead, you will have to modify the data to get the behavior you want. So, store values that are in the order you need them when sorted lexicographically. For numbers you can accomplish that by padding them with zeroes:

  • "0131" //zero added before
  • "0132" //zero added before
  • ......
  • "1308"
  • "1309"
  • "1310"
  • "1311"