Sort from the latest to the oldest in firebase

Asked

Viewed 818 times

0

Currently I am able to sort the publications by date, but as they were saved by timestamp, it is always displayed from the oldest to the newest, I would like to do the reverse, with the newest publications first. Below is my current code:

        database.ref("Pedidos").orderByChild("data_pedido").on('child_added', function(snapshot){

As the date is being saved in the bank: Imagem do banco do Firebase

Grateful for the attention.

1 answer

0


As explained in this topical firebase can sort ascendingly with the joint use of the boundToFirst (n first items) or boundToLast function (n last items).

To be able to search in descending order, there are two other options :

  • Reorder data internally in your application.
  • Create a new field and store a timestamp with the descending order, example:

    -1 * new Date(). getTime();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.