0

I'm currently writing like this

static func fromIntervals(intervals: [Interval]) -> ChartData {
    let sortedIntervals = intervals.sorted { (a, b) in return a.startTime < b.startTime}
}

But it shows error of

Cannot invoke 'sorted' with an argument list of type '((_, _) -> _)'

I searched for a lot of other code examples, and none of them work, I have no idea why. Any suggestions?

1

2 Answers 2

1

it is sortnot sorted

let sortedIntervals = intervals.sort { (a, b) in return a.startTime < b.startTime}
Sign up to request clarification or add additional context in comments.

Comments

1

You are doing it wrong. Use sort instead of sorted.

static func fromIntervals(intervals: [Interval]) -> ChartData {
    let sortedIntervals = intervals.sort { (a, b) in return a.startTime < b.startTime}
}

Edit

let sortedIntervals = sorted(intervals, { (a: Object, b: Object) -> Bool in return a.startTime < b.startTime } )

See Apple's doc for more information.

1 Comment

sort is changing the intervals itself - but that's immutable since it's the parameter of the function. I want another sorted copy.

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.