How to sort time in AM/ PM in Realm

There is one class inherited Object that includes one Date-type property.

class Entry: Object {
let date: Date = Date()
}

And there is a list of Entry objects.

let entries: List<Entry>
EntryA (date: 2020-09-01 02:00 am)
EntryB (date: 2020-09-01 03:10 pm)
EntryC (date: 2020-09-02 03:40 am)
EntryD (date: 2020-09-02 05:23 am)
EntryE (date: 2020-09-02 08:42 pm)
EntryF (date: 2020-09-03 11:04 am)
EntryG (date: 2020-09-03 13:42 pm)

I’d like to divide this list into two groups: “am” and “pm”.

The am group describes entries that have a date property between 12:00 am - 11:59 am.

The pm group describes entries that have a date property between 12:00 pm - 11:59 pm.

am Group:

EntryA (date: 2020-09-01 02:00 am)
EntryC (date: 2020-09-02 03:40 am)
EntryD (date: 2020-09-02 05:23 am)
EntryF (date: 2020-09-03 11:04 am)

pm Group:

EntryB (date: 2020-09-01 03:10 pm)
EntryE (date: 2020-09-02 08:42 pm)
EntryG (date: 2020-09-03 13:42 pm)

How can I filter the entries from the list into the am and pm groups (as shown above)?

Can you confirm what SDK you’re using using? My assumption is you’re using Swift.

Assuming that and you’re trying to filter from an already queried set of results - you will have to convert the AM/PM time format to the 24 hour format using DateFormatter and then filter by hours that are < 12hr and > 12hr.

I am sorry it took so long to get back to you and thanks for your reply!

Can you confirm what SDK you’re using? My assumption is you’re using Swift.

Yes. I am using Realm Swift/Cocoa.

Assuming that and you’re trying to filter from an already queried set of results - you will have to convert the AM/PM time format to the 24 hour format using DateFormatter and then filter by hours that are < 12hr and > 12hr.

I have already tried the approach above before and it worked well but I would like to filter the entries in Realm queries, if possible. So I can use the lazy-loading feature, which is more efficient and fast. What do you think?