排序日期为Swift 3的字典数组 [英] Sorting of array of dictionary with date Swift 3
问题描述
我有一个名为myArray的数组,其中添加了字典,我希望该字典按时间排序,这是字典中的关键.那个时间在String中.时间的日期格式为"yyyy/MM/dd HH:mm:ss"
I have an array called myArray in which dictionaries are added I want that dictionary to be sorted by time which is a key in dictionary. And that time is in String. The date format of time is "yyyy/MM/dd HH:mm:ss"
我尝试了以下代码解决方案,但警告从字符串中广播?"无关类型日期"总是失败".
I tried with below code solution but gives a warning of "Cast from 'String?' to unrelated type 'Date' always fails".
let sortedArray = self.myArray.sorted{ ($0["Time"] as? Date)! > ($1["Time"] as? Date)! }
print(sortedArray)
如果有人可以帮助我,谢谢.
If anyone can help me out, Thank You.
推荐答案
您无需为此日期转换为日期时间.您使用的国际格式(yyyy/MM/dd HH:mm:ss)提供了正确的字符串排序顺序.
You don't need to convert to date time for this sort. The international format (yyyy/MM/dd HH:mm:ss) you're using provides the right sorting order as a string.
这篇关于排序日期为Swift 3的字典数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!