1

firebase stores data according to the order of saving. Is it possible to tell firebase to store the child on top of / after this particular child? For example if I want to save the new data right before 'Fish' instead of after 'Fish' , is it possible to do so. Thanks in advance

Pets :

 Name : Cat
 ID   : 123

 Name : Dog
 ID   : 456

 Name :Fish
 ID   :789
Mia Davis
  • 131
  • 9

2 Answers2

1

If you have a specific ordering for children in mind for your app, then you should introduce a child value that sorts the way you want them to appear. You don't really have another way to "force" the ordering. It's up to you to write child values that sort the way you expect, and it's up to your client code to query them for that order.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Can I atleast have them in ascending or descending order ? If I put a value that says the way in which they should appear – Mia Davis May 25 '19 at 03:51
  • You can put two different values that sort either way. You can't do it both with one item. Realtime Database only sorts ascending. – Doug Stevenson May 25 '19 at 03:59
  • Query results are returned in ascending order of their value. If you want them reverse, you can either do that client-side after retrieving the values, or by storing an inverted value in a property. See https://stackoverflow.com/questions/25611356/display-posts-in-descending-posted-order – Frank van Puffelen May 25 '19 at 03:59
0

You have no control over the order in which Firebase stores nodes.

The console displays nodes in lexicographical order of their keys, so if you use push that means they are shown in the order you added them. There is no way to change, or reverse this.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807