Farid Al Haddad
Farid Al Haddad

Reputation: 833

Firebase sort data by date

I am trying to sort the data that i get by date, but it is not working. This is the data that i want to sort:

{
"events": {
  "Saturday, May 6, 2016" : [ null, {
    "Id" : 3,
    "LogoImage" : "image_url",
    "MainImage" : "image_url",
    "Name" : "event_name"
  }, {
    "Id" : 4,
    "LogoImage" : "image_url",
    "MainImage" : "image_url",
    "Name" : "event_name"
  } ],
  "Saturday, April 8, 2016" : [ null, {
    "Id" : 1,
    "LogoImage" : "image_url",
    "MainImage" : "image_url",
    "Name" : "event_name"
  }, {
    "Id" : 2,
    "LogoImage" : "image_url",
    "MainImage" : "image_url",
    "Name" : "event_name"
  } ]
 }
}

I want them ordered by date, but the problem is that date is the key of a list of items. How can i do that ?

I've tried this but it is not working:

ref.child("events").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in
    let results = snapshot.value as! [String: Any]
        for (eventDate, arr) in results {
            let eventsDate = EventsDate()
            eventsDate.eventsDate = eventDate
            print(eventDate)
        }
})

Thanks

Upvotes: 2

Views: 6313

Answers (2)

Travis
Travis

Reputation: 2462

Your data is being sorted by the key (date) but the problem is that your key is a string and strings are sorted alphabetically. So you're going to get all the Fridays first, then Mondays, and so on.

To sort chronologically you need to use a format that represents dates as ascending numbers, ie a timestamp. Firebase has a nifty shortcut to auto-populate timestamps in a standard format when they're written to the database, but unfortunately that only applies when you need the time at which the data is created. However it's not difficult to produce a timestamp for any arbitrary date and time, and there are plenty of libraries that will convert that back and forth to any human-readable format you like in your UI. If you're only interested in the date you can probably get away with a simple sortable format like ISO-8601:

{
"events": {
  "2016-05-06" : [ null, {
    "Id" : 3,
    "LogoImage" : "image_url",
    "MainImage" : "image_url",
    "Name" : "event_name"
  }, {
    "Id" : 4,
    "LogoImage" : "image_url",
    "MainImage" : "image_url",
    "Name" : "event_name"
  } ],
  "2016-04-08" : [ null, {
    "Id" : 1,
    "LogoImage" : "image_url",
    "MainImage" : "image_url",
    "Name" : "event_name"
  }, {
    "Id" : 2,
    "LogoImage" : "image_url",
    "MainImage" : "image_url",
    "Name" : "event_name"
  } ]
 }
}

It's also highly inadvisable what you're doing with arrays in the data, but that's for another topic.

Upvotes: 4

Oprimus
Oprimus

Reputation: 1898

Firebase has its own keys (the UID it generates for you) and prioritizes those by default with the orderedBy_ query. These default keys are prioritized by time/date added.

What you (likely) want is to order by "child", which are essentially your own keys. Try the following:

ref.queryOrdered(byChild: "Id") 

or whatever other child key you like.

Upvotes: 0

Related Questions