Reputation: 1341
I shall like to sort out an array in order chronological. I know not at all how to take myself there ... Here is the array:
[["FM", "TEL", "ID", "2017-06-17 18:16:29 +0000", "TYPE", "inbox"], ["FM", "TEL", "ID", "2017-06-17 18:17:24 +0000", "TYPE", "no", "send"], ["Jm", "TEL", "ID", "2017-06-17 19:25:27 +0000", "TYPE", "no", "send"]]
I tried with that but that did not work ....
str.sort({$0[3].date > $1[3].date})
I shall like the most recent date at the beginning, the output should be like this:
[["Jm", "TEL", "ID", "2017-06-17 19:25:27 +0000", "TYPE", "no", "send"], ["FM", "TEL", "ID", "2017-06-17 18:17:24 +0000", "TYPE", "no", "send"], ["FM", "TEL", "ID", "2017-06-17 18:16:29 +0000", "TYPE", "inbox"], ["FM", "TEL", "ID", "2017-06-17 18:17:24 +0000", "TYPE", "no", "send"]]
Upvotes: 1
Views: 510
Reputation: 1252
Use dateformatter to convert strings into date first and then compare them:
let str = [["FM", "TEL", "ID", "2017-06-17 18:16:29 +0000", "TYPE", "inbox"], ["FM", "TEL", "ID", "2017-06-17 18:17:24 +0000", "TYPE", "no", "send"], ["Jm", "TEL", "ID", "2017-06-17 19:25:27 +0000", "TYPE", "no", "send"]]
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let sorted = str.sorted(by: {dateFormatter.date(from: $0[3])! > dateFormatter.date(from: $1[3])!})
Upvotes: 1