Swift 按日期对自定义词典排序

Swift 按日期对自定义词典排序,swift,sorting,dictionary,Swift,Sorting,Dictionary,这是我在Firestore中的数据: 我的评论模式: class CommentModel { var commentText: String? var commentDate: NSObject? var uid: String? var username : String? var profileImageUrl: String? init(dictionary: [String: Any]) { commentText = dictionary["commentText"]

这是我在Firestore中的数据:

我的评论模式:

class CommentModel {

var commentText: String?
var commentDate: NSObject?
var uid: String?
var username : String?
var profileImageUrl: String?

init(dictionary: [String: Any]) {
    commentText = dictionary["commentText"] as? String
    commentDate = dictionary["commentDate"] as? NSObject
    uid = dictionary["uid"] as? String
    username = dictionary["username"] as? String
    profileImageUrl = dictionary["profileImageUrl"] as? String
    }
}
我的评论词典包含所有“评论”文档

XCode给了我以下建议,但我不确定如何使用它

self.comments.sorted { (CommentModel, CommentModel) -> Bool in

                }
如何按注释日期对词典进行排序?

您需要

self.comments.sort { $0.commentDate  < $1.commentDate  }


将日期存储为
日期
。不是作为一个
NSObject?
NSObject
是一个非常通用的类型。具体到类型Shi Sh_Khan,我已经尝试过了,并且得到了这个结果-无法将类型“()”的值赋给类型“[CommentModel]”排序没有返回它的变化
commentDate = dictionary["commentDate"] as? Date
commentDate = dictionary["commentDate"] as? String