在开发过程中,有需求是根据json中的数据进行时间排序,代码如下:
//dataArray:[OperationMaintenanceObject] 对象数组
//根据时间进行排序
self.operationMaintenanceList = self.operationMaintenanceList?.sorted(by: { (array1, array2) -> Bool in
let obj1 = (array1 as! [OperationMaintenanceObject]).first
let obj2 = (array2 as! [OperationMaintenanceObject]).first
let dateFormater = DateFormatter()
dateFormater.dateFormat = "YYYY-MM-dd"
let date1 = dateFormater.date(from: (obj1?.releaseTime)!)
let date2 = dateFormater.date(from: (obj2?.releaseTime)!)
return date1?.compare(date2!) == .orderedDescending
})
版权声明:转载 https://blog.csdn.net/ZY_FlyWay/article/details/79462362
网友评论