0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.