Display data in order by date IONIC/FIREBASE

Hello,

I have one more thing to finish my application …

I use ionic with Firebase and I post comments on a page! However, I can not display them in order by date. They appear in the total order (two hours,one minute,three hours…).

Thanks …

You can try like this

firebase.database().ref(endpoint)
  .orderByChild('timestamp')
  .on('value', function(snapshot) {
  this.data = [];

  snapshot.forEach(function(child) {
    this.data.push(child.val());
  }.bind(this));

  console.log("all", data.map(function(val) { return new Date(val.timestamp).toString(); }));
});

Good Luck
Thanks

2 Likes

Also you can try like this

posts.component.ts

export class PostsComponent {
  posts: FirebaseListObservable<any>;

  constructor(db: AngularFireDatabase) {
    this.posts = db.list('/posts', {
      query: {
        orderByChild: 'date'
      }
    });
  }
}

posts.component.html

<div *ngFor="let post of (posts | async)?.slice().reverse()">
  <h1>{{ post.title }}</h1>
</div>
4 Likes

It’s working fine !! Thanks you … !! :slight_smile:

1 Like

if its work fine please mark the solution.

What’s this “endpoint”?

You enlightened me!
It really helped me a lot.

Thanks but i am new in ionic 3, how can we use firebase for filtering data order by date please can you explain it.

please your can try this one