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?

firstprayer
  • 808
  • 2
  • 11
  • 18
  • Is `startTime` an `NSDate`? Then this is a duplicate of http://stackoverflow.com/questions/26577496/how-do-i-sort-a-swift-array-containing-instances-of-nsmanagedobject-subclass-by: `NSDate`s cannot be compared with `<` directly. – Martin R Jun 23 '15 at 05:32

2 Answers2

1

it is sortnot sorted

let sortedIntervals = intervals.sort { (a, b) in return a.startTime < b.startTime}
Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32
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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • sort is changing the intervals itself - but that's immutable since it's the parameter of the function. I want another sorted copy. – firstprayer Jun 23 '15 at 05:18