Using Swift 4 & Swift 3
let testArray = ["25 Jun, 2016", "30 Jun, 2016", "28 Jun, 2016", "2 Jul, 2016"]
var convertedArray: [Date] = []
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MM, yyyy"// yyyy-MM-dd"
for dat in testArray {
let date = dateFormatter.date(from: dat)
if let date = date {
convertedArray.append(date)
}
}
var ready = convertedArray.sorted(by: { $0.compare($1) == .orderedDescending })
print(ready)
Using Swift 2
For example you have the array with dates and another 1 array, where you will save the converted dates:
var testArray = ["25 Jun, 2016", "30 Jun, 2016", "28 Jun, 2016", "2 Jul, 2016"]
var convertedArray: [NSDate] = []
After that we convert the dates:
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MM, yyyy"// yyyy-MM-dd"
for dat in testArray {
var date = dateFormatter.dateFromString(dat)
convertedArray.append(date!)
}
And the result:
var ready = convertedArray.sort({ $0.compare($1) == .OrderedDescending })
print(ready)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…