Arrays 如何在tableview中按日期对数组排序

Arrays 如何在tableview中按日期对数组排序,arrays,swift,xcode,uitableview,tableview,Arrays,Swift,Xcode,Uitableview,Tableview,我试图返回以下按日期排序的单元格。我已经搜索了很多帖子,但我真的不明白我具体把排序依据放在哪里: let sortedArray = jogs.sorted { $0.jogDate < $1.jogDate } 还是应该将排序放在结构文件中?类文件是否比结构文件更好?1。按日期对慢跑进行分类 override func viewDidLoad(){ super.viewDidLoad()\ jogs.sort{$0.jogDateUITableViewCell{ guard let c

我试图返回以下按日期排序的单元格。我已经搜索了很多帖子,但我真的不明白我具体把排序依据放在哪里:

let sortedArray = jogs.sorted { $0.jogDate < $1.jogDate }

还是应该将排序放在结构文件中?类文件是否比结构文件更好?

1。按日期对慢跑进行分类

override func viewDidLoad(){
super.viewDidLoad()\
jogs.sort{$0.jogDate<$1.jogDate}
tableView.reloadData()
}

2。在UITableViewDelegate/Datasource实现中使用排序数组

override func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
guard let cell=tableView.dequeueReusableCell(标识符为“ToDoCelliIdentifier”)作为?ToDoCell else{
fatalError(“无法将单元格出列”)
}
cell.delegate=self
让jog=sortedArray[indepath.row]//排序数组
cell.titleLabel?.text=jog.title
cell.descriptionLabel.text=jog.notes
cell.dateLabel.text=Jog.jogDateFormatter.string(from:Jog.jogDate)
返回单元
}

您的数据源是否总是按日期排序?如果是,则在
viewDidLoad
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoCellIdentifier") as? ToDoCell else {
            fatalError("Could not dequeue a cell")
        }

        cell.delegate = self

        let jog = jogs[indexPath.row]
        cell.titleLabel?.text = jog.title
        cell.descriptionLabel.text = jog.notes
        cell.dateLabel.text = Jog.jogDateFormatter.string(from: jog.jogDate)

        return cell
    }