-1

Im having a hard time trying to figure out how to correctly sort my Message struct. Below is a function Im using to do so, which isn't working. Im getting an "Argument passed to call that takes no arguments" error returned whenever I try to build.

@objc func handleReloadTable() {
    self.messages = Array(self.messagesDictionary.values)
    self.messages.sort(by: { (message1, message2) -> Bool in

        return message1.timestamp?.int32Value > message2.timestamp?.int32Value
    })

    DispatchQueue.main.async(execute: {
        self.tableView.reloadData()
    })
}

A bit frustrating as I've used this same function on multiple projects and I've never had an issue. However, those projects were written in Swift 3.0 and this project is in Swift 4.

enter image description here

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Vandal
  • 708
  • 1
  • 8
  • 21
  • If you want to sorting your message based on date, you may refer [here](https://stackoverflow.com/questions/38168594/sort-objects-in-array-by-date) – Nizzam Mar 31 '19 at 15:09
  • Consider to declare the timestamp as non-optional. In practice timestamps of messages are very unlikely optional. – vadian Mar 31 '19 at 15:43

1 Answers1

0

Try this formatting of the "sort" method instead:

self.messages.sort { (message1, message2) -> Bool in
    return message1.timestamp?.int32Value > message2.timestamp?.int32Value
}

Tried in swift 4 and 5

Pointblaster
  • 504
  • 3
  • 18