Skip to content Skip to sidebar Skip to footer

How To Sort Value Inside A Firebase Child To The Rest?

In my firebase I have several events, each with title and date string: { 'events': { '-JscIDsctxSa2QmMK4Mv': { 'date': 'Friday, June 19, 2015',

Solution 1:

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")...

Post a Comment for "How To Sort Value Inside A Firebase Child To The Rest?"