Trying to retrieve Firebase list query in reverse order

671 Views Asked by At

So I am trying to retrieve a list of players scores in order. I am using a query with Firebase list observable, It works, but it starts at 0 and goes up. Instead, I want to start with the largest on down. I tried using -score but that did not work either.

Code:

 this.PlayerList=this.AngularFire.database.list(`/Rooms/PList`, {
       query:{
         orderByChild:'score',
      }
     });
1

There are 1 best solutions below

1
On BEST ANSWER

You can't set the order direction in Firebase. The options are to sort locally, which is not much use if you are paging or to create a child property to sort on. So for your case you could keep have a scoreOrder property with the value always set to the negative score and then sort on that to get the results to return with the highest scorer first.

  {
    "p1": {
      "score": 1,
      "scoreOrder": -1
    },
    "p2": {
      "score": 2,
      "scoreOrder": -2
    },
    "p3": {
      "score": 3,
      "scoreOrder": -3
    }
  }

See this link for more details https://groups.google.com/forum/#!topic/firebase-talk/nigfh-ekIm8