angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Query ordered by ascending / descending

Open funnytao opened this issue 10 years ago • 9 comments

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.

funnytao avatar Jul 07 '16 21:07 funnytao

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

lf-alves avatar Jul 08 '16 16:07 lf-alves

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.

funnytao avatar Jul 08 '16 16:07 funnytao

https://github.com/angular/angularfire2/issues/283#issuecomment-231511751

justme1 avatar Jul 09 '16 19:07 justme1

This question should handle it: http://stackoverflow.com/questions/25611356/display-posts-in-descending-posted-order

davideast avatar Jul 15 '16 18:07 davideast

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 😄

jdell64 avatar May 28 '17 14:05 jdell64

Oh nm... found it... you can make a map.

jdell64 avatar May 28 '17 14:05 jdell64

ref.orderBy('price', 'desc')); ref.orderBy('price', 'asc'));

niojihye avatar Mar 07 '18 01:03 niojihye

@niojihy is this: ref.orderBy('price', 'desc')); ref.orderBy('price', 'asc'));

working for you?

Sireini avatar Mar 30 '18 15:03 Sireini

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

RHeckers avatar Jun 08 '18 08:06 RHeckers