I'm trying to test out Firebase to allow users to post comments using push
. I want to display the data I retrieve with the following;
fbl.child('sell').limit(20).on("value", function(fbdata) {
// handle data display here
}
The problem is the data is returned in order of oldest to newest - I want it in reversed order. Can Firebase do this?
I did this by prepend.
Firebase supports retrieving child nodes of a collection in two ways:
What you're getting now is by name, which happens to be chronological. That's no coincidence btw: when you
push
an item into a collection, the name is generated to ensure the children are ordered in this way. To quote the Firebase documentation forpush
:The Firebase guide on ordered data has this to say on the topic:
The simplest way to get the behavior you want is to also specify an always-decreasing priority when you add the item:
Update
You'll also have to retrieve the children differently:
In my test using
on('child_added'
ensures that the last few children added are returned in reverse chronological order. Usingon('value'
on the other hand, returns them in the order of their name.Be sure to read the section "Reading ordered data", which explains the usage of the
child_*
events to retrieve (ordered) children.A bin to demonstrate this: http://jsbin.com/nonawe/3/watch?js,console
There is really no way but seems we have the recyclerview we can have this
myarray.reverse(); or this.myitems = items.map(item => item).reverse();
You can add a column named orderColumn where you save time as
Long refrenceTime = "large future time"; Long currentTime = "currentTime"; Long order = refrenceTime - currentTime;
now save Long order in column named orderColumn and when you retrieve data as orderBy(orderColumn) you will get what you need.
In Android there is a way to actually reverse the data in an Arraylist of objects through the Adapter. In my case I could not use the LayoutManager to reverse the results in descending order since I was using a horizontal Recyclerview to display the data. Setting the following parameters to the recyclerview messed up my UI experience:
The only working way I found around this was through the BindViewHolder method of the RecyclerView adapter:
Hope this answer will help all the devs out there who are struggling with this issue in Firebase.