2

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
5
  • Can't you just do 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. Commented Jun 15, 2019 at 14:23
  • Yes, but I am getting error "Value of type '[String]?' has no member 'sort'" Commented Jun 15, 2019 at 14:24
  • Please advise me @Sweeper Commented Jun 15, 2019 at 14:25
  • 1
    Do you know anything about optionals? If not, go learn about them. They are pretty important in Swift. You should do artEventTime?.sort(). Commented Jun 15, 2019 at 14:30
  • 1
    A better question would be why do you have an array of dates as strings instead of an array of actual Date instances? 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. Commented Jun 15, 2019 at 20:55

3 Answers 3

3

I would not advise making it a practice doing lexicographic sorting on dates. It's simple enough just to parse these strings to proper Date objects and sort on those:

let dateStrings = Optional(["2019-07-08", "2019-07-09", "2019-07-10", "2019-07-11", "2019-07-12", "2019-07-02"])

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let sortedDates = dateStrings?.compactMap(formatter.date(from:)).sorted()
Sign up to request clarification or add additional context in comments.

3 Comments

Why, dates given in this format will sort perfectly fine as strings.
For the example strings provided, this is true, but throw in a date from AD 30, e.g. "30-07-08" and lexicographic sort breaks. Though this is contrived, my point is simply that you're making an implicit assumption about your date strings that may not always be true in the future. One of our jobs as developers is to eliminate assumptions or make them explicit.
Another of our jobs is to not over engineer or over complicate things and find the most suitable solution for the problem at hand.
1

I got the answer:

let sortedArray = aryEventTime.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedAscending }

2 Comments

This shouldn't work if aryEventTime is optional and looks overly complicated, why not aryEventTime?.sort as mentioned in the comments above or let sortedArray = aryEventTime?.sorted()
This is the hard way. Since your date strings are yyyy-MM-dd, you can do: let sortedArray = aryEventTime.sorted { $0 < $1) or just let sortedArray = aryEventTime.sorted().
0

The array of String that you have is,

let arr = ["2019-07-08", "2019-07-09", "2019-07-10", "2019-07-11", "2019-07-12", "2019-07-02"]

Instead of converting the String to Date, you can simply sort this array using sorted(), i.e.

let sortedArr = arr.sorted()
print(sortedArr)

Output:

["2019-07-02", "2019-07-08", "2019-07-09", "2019-07-10", "2019-07-11", "2019-07-12"]

Note: date strings can only be sorted without conversion in special cases such as this one. If the strings were in the format yyyy-dd-MM, for example, then simple string sorting would not work.

1 Comment

It should be noted that date strings can only be sorted without conversion in special cases such as this one. If the strings were in the format yyyy-dd-MM, for example, then simple string sorting would not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.