orderby firebase code example

Example 1: orderBy firebase angular

import { map, take } from 'rxjs/operators';
import { Observable } from 'rxjs';

public allProducts: Observable<any[]>;
 public amazonCollection: AngularFirestoreCollection;  

this.amazonCollection = this.afs.collection('amazon', (ref) =>
      ref.orderBy('voteCount')
    );
    this.allProducts = this.amazonCollection.snapshotChanges().pipe(
      map((actions) => {
        return actions.map((a) => {
          return { id: a.payload.doc.id, ...a.payload.doc.data() };
        });
      })
    );

Example 2: order documents in firestore

// Create Data
const timestamp = firebase.firestore.FieldValue.serverTimestamp;

db.collection('things').add({ ...myData, createdAt: timestamp() })


// Query
db.collection('things').orderBy('createdAt').startAfter(today)