I want to sort an array in ascending order. The dates are in string format
Optional(["2019-07-08", "2019-07-09", "2019-07-10", "2019-07-11", "2019-07-12", "2019-07-02"])
I am trying using below code but it's not working.
aryEventTime = aryEventTime.sorted(by: { ($0 as AnyObject).date.compare($1.date) == ComparisonResult.orderedAscending })
aryEventTime = aryEventTime.sorted(by: {
($0 as AnyObject).date.compare(($1 as AnyObject).date) == .orderedDescending}) as? NSMutableArray
aryEventTime.sort()? That should do the job. Your dates are in a format such that sorting lexicographically will result in the same order as sorting in date order.artEventTime?.sort().Dateinstances? You should not work with dates as strings. The only time you should have a date as a string is to display it to the user and it should be shown in a format appropriate to the user's locale.