how to implement descending orderByChild()

Skip to first unread message

Raj Virk

Jul 24, 2015, 10:39:23 AM7/24/15
to Firebase Google Group

i have data where one child keeps value of most likes, i want to order or show data in descending order as below. please help me to implement this.

var ref = new Firebase(<LINK>);

// i have around 200 items to show in a way that most likes comes on top all the time

ref.orderByChild('likes').on("child_added", function(snapshot) {
  var data = snapshot.val();
document.getElementById('data').innerHTML = html.join('');

Please help with above scenario.


Michael Lehenbauer

Jul 24, 2015, 11:59:01 AM7/24/15
Hi Raj,

The basic approach here is to write your code to just render the items in reverse order.  For an example of that you can take a look at our leaderboard example.  It shows high scores from high to low.

Let me know if you run into issues.


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
To post to this group, send email to
To view this discussion on the web visit
For more options, visit

Raj Virk

Jul 24, 2015, 11:31:35 PM7/24/15
to Firebase Google Group,
Hello Michael,

Thanks for replying back, but can you please give me example of code i provided up there to show most likes on top and than lowest at bottom. 

Thanks in advance.


Frank van Puffelen

Jul 24, 2015, 11:54:08 PM7/24/15
to Firebase Google Group,,
Hey Raj,

The leaderboard example that Michael linked to does precisely that: it shows the player with the highest score at the top and the lowest score at the bottom. See

It uses priorities to determine the order, but you can nowadays accomplish the same with any property. The trick to get descending order is to invert the number of likes, so:

item.order = -1 * item.likes

Then you can run a query that gets the number of item you want to show:

ref.orderByChild("order").limitToFirst(10).on("child_added", function(snapshot, previousChildKey) {

On the initial load, the children will arrive in the correct order. So it is tempting to simply append them to your data. But after the initial data has loaded, the query will still be monitoring for changes. Once an item gets a higher number of likes than are currently in the data, you will get a child_removed event for the least-liked item and a child_added for the new favorite item.

The leaderboard sample handles this in the best way: when Firebase calls you child_added callback, it passes a previousChildKey. You can use this to look up the previous child's element in the HTML and insert the new favorite item in its correct place.

Raj Virk

Jul 25, 2015, 10:52:49 AM7/25/15
to Firebase Google Group,,

Thanks for replying, But i get the similar results with css hack. which is very easy to implement. I am getting my results in how i really want, hope this can help others too or you can put this hack on firebase knowledge base if you think this works accordingly without any issue..

for eg: you are rendering all innerhtml results in a div called container

<div id = "container">
   <!-- firebase results here -->

-- css---
  display: -webkit-flex;
        display: -moz-flex;
        display: -ms-flex;
        display: -o-flex;  
        -webkit-flex-direction: column-reverse;
        -moz-flex-direction: column-reverse;
        -ms-flex-direction: column-reverse;
        -o-flex-direction: column-reverse;

fb.orderByChild('likes').on('child_added', function(sp){
  data = sp.val();
  document.getElementById('container').innerhtml = html.join('');
Reply all
Reply to author
0 new messages