按日期顺序对数组进行排序,并在表视图中显示包含节的数组值

时间:2016-11-22 11:26:48

标签: swift sorting

这是我的模特课

class Messages: NSObject {

    var id: Int?
    var sender_id: Int?
    var group_id: Int?
    var message_type: Int?
    var message: String?
    var sent_time: Date?
    var status: Int?

    init(dictionary: [String: AnyObject]) {
         super.init()

        self.id = (dictionary["id"] as! NSString).integerValue
        self.sender_id = (dictionary["sender_id"] as! NSString).integerValue
        self.group_id = (dictionary["group_id"] as! NSString).integerValue
        self.message_type = (dictionary["message_type"] as! NSString).integerValue
        self.message = dictionary["message"] as! String?
        let inputTime = dictionary["sent_time"] as! String?
        let inputDateFormatter = DateFormatter()
        let inputTimeZone = TimeZone(abbreviation: "UTC")
        inputDateFormatter.timeZone = inputTimeZone
        inputDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let dateFromString = inputDateFormatter.date(from: inputTime!)
        self.sent_time = dateFromString
        self.status = (dictionary["status"] as! NSString).integerValue

    }


}

这是我的模型类对象数组

var messages = [Messages]()

数组按升序排序

self.messages.sort(by: { $0.sent_time! < $1.sent_time! })

如何为日常创建不同的数组,这个数组存储在字典中,并在表格视图中查看,节日标题为foe day

看起来像这样

enter image description here

0 个答案:

没有答案