4

In my firebase I have several events, each with title and date string:

{
    "events": {
        "-JscIDsctxSa2QmMK4Mv": {
            "date": "Friday, June 19, 2015",
            "title": "Event Two"
        },
        "-Jswff0o9bWJeDmUoSA9": {
            "date": "Friday, June 12, 2015",
            "title": "Event One"
        },
        "-JscIs_oMCJ9aT6-JWDg": {
            "date": "Monday, August 10, 2015",
            "title": "Event Three"
        }
    }
}

In my javascript code, I retrieve the events child and push each title and date to an array then append it to my html page and display the content.

var ref = new Firebase("https://demo.firebaseio.com/events");
var build = new Array("");
ref.orderByChild("date").once("value", function(snapshot) {
  snapshot.forEach(function(data) {
    var tmp = data.val();
    eventMonth = tmp.date.split(" ")[1];
    build.push('<h3>'+tmp.title+'</h3><p>Date: '+tmp.date+'</p>');
  });
  $("#event-content").append(build.join(''));

orderByChild doesn't seem to be working, how can I order the events by date so it can look something like below:

Event One: Friday, June 12, 2015

Event Two: Friday, June 19, 2015

Event Three: Monday, August 10, 2015

Ricek
  • 645
  • 3
  • 12
  • 22
  • hey, how are you saving the data in database? Like what is `-JscIDsctxSa2QmMK4Mv`, `-Jswff0o9bWJeDmUoSA9` and `-JscIs_oMCJ9aT6-JWDg`? – Hammad Nasir Dec 14 '16 at 05:42

1 Answers1

6

Firebase doesn't have a date type, since JSON doesn't have it. So it has no idea that there're stored dates in those strings. For sorting purposes you have to store a primitive type that represents these dates and gives required order of sorting when compared as a string or number.

For example: a timestamp. Given date is a JS date object, add sortDate: date.getTime() to each object when saving.

{
  "events": {
    "-JscIDsctxSa2QmMK4Mv": {
        "date": "Friday, June 19, 2015",
        "sortDate": 1434697200000,
        "title": "Event Two"
    },
    "-Jswff0o9bWJeDmUoSA9": {
        "date": "Friday, June 12, 2015",
        "sortDate": 1434092400000,
        "title": "Event One"
    },
    "-JscIs_oMCJ9aT6-JWDg": {
        "date": "Monday, August 10, 2015",
        "sortDate": 1439190000000,
        "title": "Event Three"
    }
  }
}

And then:

ref.orderByChild("sortDate")...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kirill Slatin
  • 6,085
  • 3
  • 18
  • 38
  • The date are already convert into string before I stored it into the Firebase – Ricek Aug 10 '15 at 00:58
  • That's exactly what I mean. From the sample data you presented it happens, for example that "Friday, June 22, 2014" comes before "Monday, August 11, 2013" because it's just string comparison and "F" goes before "M" – Kirill Slatin Aug 10 '15 at 02:16
  • I tried something similar but I'm not getting a sorted response. Can you check this question out? It's very similar http://stackoverflow.com/questions/43459819/retrieve-from-firebase – Anil Apr 17 '17 at 21:59
  • 1
    How do I order by `desc` order? – Hussain Jun 06 '17 at 06:11
  • 1
    @Hussain, Firebase doesn't have Descending in its API. You should implement some tricks, like adding items in reverse order when processing retrieved results, similar to [this](https://groups.google.com/forum/#!topic/firebase-talk/nigfh-ekIm8) or [this](https://stackoverflow.com/questions/38303830/how-to-retrieve-data-from-firebase-database-in-descending-order) – Kirill Slatin Jun 06 '17 at 06:49