0

I am working with firebase real time database. Every time I push the data into the firebase using push method it auto generates a key like this "-L1GgaMStpwEV4N3sQad" to my best knowledge it's generated based on time also.

So my question is there any way to sort the key asce or in desc?.

I have attached image of firebase auto generated.

enter image description here

Thanks in advance.

KENdi
  • 7,576
  • 2
  • 16
  • 31
  • It's a bit counter intuitive if you're used to SQL but you normally just reverse the results in javascript. – iSZ Mar 14 '18 at 11:12
  • how to do that? I'm new to this. –  Mar 14 '18 at 11:22
  • 1
    they are already saved in ascending order of timestamp why not use javascript code to reverse the order. – Himanshu Mar 14 '18 at 14:14
  • @Daffodil Firebase push IDs are defined so that their lexicographical order is also the chronological order in which they were generated. If you want to reverse them, you'll do this client-side in your own code. How precisely to do this, depends on how you've already loaded the data. We can't help without seeing what you've already done (known as an [MCVE](http://stackoverflow.com/help/mcve)). – Frank van Puffelen Mar 14 '18 at 14:32
  • 1
    I would suggest not using keys for that. Create push id's as a key and create two child nodes under each that stores numerical timestamps. One node is a timestamp and the other node is a 1-timestamp. Then they can be loaded ascending or descending according to those timestamps and are not tied to the key. The other benefit is they are actual timestamps, if you need that functionality. Optionally, @frankvanpuffelen mentions, you can always sort in code. [Sort Descending](https://stackoverflow.com/questions/36589452/in-firebase-how-can-i-query-the-most-recent-10-child-nodes/36665442#36665442) – Jay Mar 14 '18 at 17:26

1 Answers1

0

I new to firebase but here's my observation : The unique keys that are generated are already sorted in Lexicographical order. Have a closer look at your objects: -L1GgaM is common for all keys (except last which ends with 'N')

Also, remember that capital letters have lower ASCII value than their counterpart.