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?
You are searching limitTolast(Int x) .This will give you the last "x" higher elements of your database (they are in ascending order) but they are the "x" higher elements
if you got in your database {10,300,150,240,2,24,220}
this method:
will retrive you : {150,220,240,300}
I'm using ReactFire for easy Firebase integration.
Basically, it helps me storing the datas into the component state, as an
array
. Then, all I have to use is thereverse()
function (read more)Here is how I achieve this :
I have a date variable (long) and wanted to keep the newest items on top of the list. So what I did was:
just use
reverse()
on the array , suppose if you are storing the values to an arrayitems[]
then do athis.items.reverse()
Someone has pointed out that there are 2 ways to do this:
The easiest way that I have found to do this is to use option 1, but through a
LinkedList
. I just append each of the objects to the front of the stack. It is flexible enough to still allow the list to be used in aListView
orRecyclerView
. This way even though they come in order oldest to newest, you can still view, or retrieve, newest to oldest.