将字符串“2016-09-02 12:25:08”转换为日期2016-09-02 12:25:08在Swift中

问题描述:

我有一组包含Key和Value的字典。我需要根据日期以降序排列词典数组,如formate 2016-09-02 12:25:08。将字符串“2016-09-02 12:25:08”转换为日期2016-09-02 12:25:08在Swift中

+0

不是卫冕,但降序排列。 –

+1

你被困在哪里? – holex

以下是可能的解决方案按日期排序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是数组排序

+0

是啊,现在它的工作,非常感谢 –

+0

如果这个答案对你来说是正确的,而不是接受它作为一个正确的答案。 – Max