Sort AngularFire2 items in order of creation date

1.5k Views Asked by At

I am new to angular and currently creating a project using Angular2 and Firebase.

I want to be able to sort my database in date created order. However, I am unsure on how I could achieve this. I looked up articles and seen their implementations. But I do not know how to implement this to my project.

Here is my code that deals with querying. My Firebase database just has title, content and date(I type the date manually).

posts: FirebaseListObservable<any>;
constructor(public navCtrl: NavController, public alertCtrl:AlertController, angFire: AngularFire) {
this.posts= angFire.database.list('/Post',{
  query:{
    orderByChild: "date"
  }
});

Any help would be great, thankyou.

JSON here

{`"Post" : {
  "-KYsuclduIKOVi6VXGmn" : {
     "content" : "text goes here",
     "date" : "12/12/16",
     "title" : "Title"
}  

} }

1

There are 1 best solutions below

0
On BEST ANSWER

So all I had to do was add the firebase.database.ServerValue.TIMESTAMP to the date. This adds the timestamp to my firebase database.

text:"Save Post",
      handler: data => {
        this.posts.push({
          title: data.title,
          content: data.content,
          date: firebase.database.ServerValue.TIMESTAMP
        })