【问题标题】:Convert the string "2016-09-02 12:25:08" into date as 2016-09-02 12:25:08 in Swift在 Swift 中将字符串“2016-09-02 12:25:08”转换为日期为 2016-09-02 12:25:08
【发布时间】:2017-01-26 19:54:49
【问题描述】:

我有一系列带有键和值的字典。我需要根据日期对字典数组进行降序排序,例如甲酸盐 2016-09-02 12:25:08。

【问题讨论】:

  • 不是防守而是降序。
  • 你被困在哪里了?

标签: swift ios9


【解决方案1】:

这是按日期对 ArrayOfDictionary 进行排序的可能解决方案:

    let sortedRostersByDate  = TempArray.sortedArray(comparator: { (obj1, obj2) -> ComparisonResult in


                        var date2:NSDate!
                        var date1:NSDate!

                        if let dict = obj2 as? NSDictionary
                        {
                            if let tempdate2 = df.date(from: dict.object(forKey: "Sectiontitle") as! String)
                            {
                                date2 = tempdate2 as NSDate!
                            }
                        }
                        if let dict = obj1 as? NSDictionary
                        {
                            if let tempdate1 = df.date(from: dict.object(forKey: "Sectiontitle") as! String)
                            {
                                date1 = tempdate1 as NSDate!
                            }
                        }
                        return date2.compare(date1 as Date)


                    })
let SortedArray = NSMutableArray(array: sortedRostersByDate)

这里的 TempArray 是要排序的数组

【讨论】:

  • 如果这个答案对你来说是正确的,而不是接受它作为正确的答案。
最近更新 更多