Getting DESC order with Angular2 and Firebase

I never thought this would be an issue… But I found that this simple need to get data from a db in DESC order was not “out of the box” with Firebase.

One comment on a Github question on this subject worked for me.  It’s one line of code and it simply works… way better than other more complex solutions.

Basically, you take your Observable, such as the one below:

this.posts = this.af.database.list('/posts') as FirebaseListObservable<Post[]>;

You can modify it with a map that runs a reverse method on it:

this.posts = this.af.database.list('/posts').map( (arr) => {return arr.reverse();}) as FirebaseListObservable<Post[]>;

 

Getting DESC order with Angular2 and Firebase
User Rating: 0 (0 votes)