Hi,
I am learning Angular 2 recently and I find angularfire2 really useful and handy. However, I don't know whether there is a way to sort the query data by descending. Do I miss anything or it is just not an option for query? Thank you so much.
This is a very common issue for most firebase users who try any type of advanced querying. This issue has been discussed a lot in the past and there's a couple of different solutions for this problem, most of which were given by firebase team members.
You can check all the SO questions regarding this topic here
Hi, I just don't want to make things get complicated, so I just write a pipe to sort and filter the data I want. Thank you so much.
This question should handle it: http://stackoverflow.com/questions/25611356/display-posts-in-descending-posted-order
So, the solution is to make sure that you push the data in the order you will want later?
I am just checking, not passing judgement 馃槃
Oh nm... found it... you can make a map.
ref.orderBy('price', 'desc'));
ref.orderBy('price', 'asc'));
@niojihy is this:
ref.orderBy('price', 'desc'));
ref.orderBy('price', 'asc'));
working for you?
This is how I solved it:
First I made a query in my service where I filter by date in milliseconds:
getImages (): Observable<Image[]> {
this.imageCollection = this.asf.collection<Image>('/images', ref => ref.orderBy('time').startAt(1528445969388).endAt(9999999999999));
this.images = this.imageCollection.snapshotChanges().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data() as Image;
const id = a.payload.doc.id;
return { id, ...data };
}))
);
return this.images;
}
Then to get the newest date first I added this to my component where I call the method from my service:
let date = new Date;
let time = 9999999999999 - date.getTime();
console.log(time);
I pass the time let as the date. Since a newer date will be a bigger number to deduct from the 9999999999999, the newest date will turn up first in my query inside my service.
Hope this solved it for you
Most helpful comment
ref.orderBy('price', 'desc'));
ref.orderBy('price', 'asc'));