Firebase Descending or Ascending order

667 views
Skip to first unread message

Daveyon Mayne

unread,
Sep 4, 2015, 2:53:07 AM9/4/15
to Firebase Google Group
I have created a momentjs timestamp and push it to my database:

var createdAt = moment().format();

So my db looks like:

9c5667-6767-978789889
 
- JyM7979898686
   
- createdAt: "2015-09-04T07:22:58+01:00"
   
- sample: "1"


2c3667-6767-008674889
 
- JyO1233498000
   
- createdAt: "2015-09-04T07:30:00+01:00"
   
-sample:"2"

When I use

order-by-child="sample"

I get the order (from lowest to highest) but I want the createdAt. So basically I want  `2015-09-04T07:30:00+01:00` to show first in my list. Is this possible?


Kato Richardson

unread,
Sep 4, 2015, 11:06:09 AM9/4/15
to Firebase Google Group
Hi Daveyon! Great to hear from you again.

This has been asked extensively on Stack Overflow, so I'll try not to duplicate those examples here.

Essentially, you'll want to to add the timestamp as a negative value in order be able to sort descending. This is a feature that will eventually make it into the core SDK, but for now, this is the only option.

var createdAt = 0 - moment().valueOf();

As a general rule of thumb for dates: numbers are going to be easier to sort and deal with in a database than strings (which sort lexicographically and not numerically). See sorting criteria for more.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/eb9d1219-959e-4279-964b-cb68fdbb8dbc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daveyon Mayne

unread,
Sep 4, 2015, 1:23:18 PM9/4/15
to Firebase Google Group
Nice little trick, Kato.

I'll keep an eye out on their new release. Thanks again.

Daveyon
Reply all
Reply to author
Forward
0 new messages