I am having a model array say [Conversation]
.
In the conversation
model object there is another model array say [Message]
.
In Message
model object there is a property say createdOn
So I am trying to sort conversation
array in descending order as per property createdOn
(a Date property) which is in Message model.
I have sorted Message array successfully. but I am having no luck in sorting conversation
array.
let msgArray = Message.mr_findAll() as? [Message]
let sortDescriptor = NSSortDescriptor(key: "createdOn", ascending: false)
let orderedArray = (msgArray as? NSArray)?.sortedArray(using: [sortDescriptor]) as! [Message]
P.S.
I am using Magical records for database.
I assume that a conversation has many messages and that you want to sort it by the last message sent like most chat app do. If that is the case, I would recommend storing the last updated date in the conversation object and updating it whenever a message is inserted.