按日期排序字符串数组

时间:2017-06-13 20:48:31

标签: swift

我有var sortedSections = [String]()日期字符串,如:

["Jul 18, 2017", "Jun 13, 2017", "Jun 15, 2017"]

是否有最佳(Swifty)方式按日期对此进行排序,以便对订单进行排序(如此?)...

["Jun 13, 2017", "Jun 15, 2017", "Jul 18, 2017"]

无论我在尝试什么都太复杂了:

var convertedArray: [Date] = []
for sorted in (self?.sortedSections)! {
    let date = String.dateFromString(dateString: sorted)
    convertedArray.append(date)
}
convertedArray.sort(by: {$0.compare($1) == .orderedAscending})
print(convertedArray)

for sorted in convertedArray {
    let date = Date.dateToString(date: sorted)
    sortedSections.append(date) // Except this is adding it to the array which is not right
}

0 个答案:

没有答案