Reputation: 761
I am trying to sort entries in my firebase by the most recent entry showing up as the first entry. I keep running into the issue that the most recent entry shows up last.
I am trying to sort by time as well as this is how it is set up in my databases:
I am also doing this in my code
completed.child(user.uid).orderByValue("time")
I was wondering where I was going wrong
This is how it shows up in my app:
Upvotes: 1
Views: 310
Reputation: 599686
There are quite a few problems with your code, so I'll try to address them in turn.
orderByChild
You're calling orderByValue("time")
, but if you check the documentation you'll note that orderByValue()
takes no parameters. If you want to order on the value of a child property, you should call orderByChild("time")
.
Your current time format is not suited for sorting it. The problem is that your lexicographical sort order doesn't match with the chronological sort order you want. More simply put, this is the sort order you'll get (but not want):
"6:26AM"
"6:26PM"
"6:27AM"
To get the result you want, use a string format that allows the sorting. For example, use 24 hour/military notation with 0-padding:
You could also store a numerical value, for example: the number of minutes since midnight:
Firebase always returns results in ascending order. I'll recommend reading a few relevant questions, but you have two options here:
This last one is typically easiest if you use a numerical value, like the number of minutes since midnight that we used above. If we store -1 *
this value, we can then sort on that to get the results in reverse.
Upvotes: 1