slimboy
slimboy

Reputation: 1653

Grab the highest date inside of an array of entities.

I have the following array :

  let messages = (fetchedResultsController.fetchedObjects as! [Message])

essentially, each element in this array is of the entity Message, where each Message has 5 core data attributes. Notably, the attribute I care about in this case is timestamp which is of type NSDate !!!!!!. I want to grab the message with the maximum date inside of this array using the reduce function. Please do not suggest to sort the messages first. I'am trying to avoid that. Thank you !

Upvotes: 0

Views: 522

Answers (1)

closetCoder
closetCoder

Reputation: 1092

Not sure why you would want to use reduce, but I think this will work and fits with what you are looking for (assuming Swift 3):

let result = messages.max(by: {$0.timeStamp < $1.timeStamp})

Upvotes: 4

Related Questions