Reputation: 73
I have a point though it is prompt.
It's about comparing dates (string?).
For example, when there are data of 12/1 and 12/5 character strings, I'd like to compare them and obtain data of 12/2, 12/3, 12/4.
In this case, I would appreciate it if you could give me advice on how to generate it.
Upvotes: 0
Views: 59
Reputation: 11242
Are you trying to sort (MM/dd) String
data?
var dateArray = ["12/1", "12/3", "9/2", "10/5", "12/4"]
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd"
dateArray.sort(by: { dateFormatter.date(from: $0)! < dateFormatter.date(from: $1)! })
print(dateArray) //["9/2", "10/5", "12/1", "12/3", "12/4"]
Note: Force-unwrapping is not recommended if you are not sure the data source is of the format "MM/dd".
Upvotes: 2