Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display posts in descending posted order

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?

like image 690
user2912591 Avatar asked Sep 01 '14 18:09

user2912591


People also ask

How do I show posts in ascending order in WordPress?

Configure the plugin by going into Posts–>Categories, then click a category in which you would like to modify the post order, then select Ascending or Descending.

How do I change the order of posts in WordPress?

After activating it click into “Post Types Order” under settings and you can enable the types of posts you want the reorder interface to show up on. Then under that post type you will see a new menu called “Re-order.” You can then drag and drop the posts within according to the order you want them to appear in.


2 Answers

Since this answer was written, Firebase has added a feature that allows ordering by any child or by value. So there are now four ways to order data: by key, by value, by priority, or by the value of any named child. See this blog post that introduces the new ordering capabilities.

The basic approaches remain the same though:

1. Add a child property with the inverted timestamp and then order on that.

2. Read the children in ascending order and then invert them on the client.

Firebase supports retrieving child nodes of a collection in two ways:

  • by name
  • by priority

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 for push:

The unique name generated by push() is prefixed with a client-generated timestamp so that the resulting list will be chronologically-sorted.

The Firebase guide on ordered data has this to say on the topic:

How Data is Ordered

By default, children at a Firebase node are sorted lexicographically by name. Using push() can generate child names that naturally sort chronologically, but many applications require their data to be sorted in other ways. Firebase lets developers specify the ordering of items in a list by specifying a custom priority for each item.

The simplest way to get the behavior you want is to also specify an always-decreasing priority when you add the item:

var ref = new Firebase('https://your.firebaseio.com/sell'); var item = ref.push(); item.setWithPriority(yourObject, 0 - Date.now()); 

Update

You'll also have to retrieve the children differently:

fbl.child('sell').startAt().limitToLast(20).on('child_added', function(fbdata) {   console.log(fbdata.exportVal()); }) 

In my test using on('child_added' ensures that the last few children added are returned in reverse chronological order. Using on('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

like image 104
Frank van Puffelen Avatar answered Sep 20 '22 10:09

Frank van Puffelen


Since firebase 2.0.x you can use limitLast() to achieve that:

fbl.child('sell').orderByValue().limitLast(20).on("value", function(fbdataSnapshot) {    // fbdataSnapshot is returned in the ascending order   // you will still need to order these 20 items in   // in a descending order } 

Here's a link to the announcement: More querying capabilities in Firebase

like image 43
Maksymilian Majer Avatar answered Sep 19 '22 10:09

Maksymilian Majer