This is partly due to the fact that the Swift compiler does not give you a useful error. The real problem is that NSDate cannot be compared to < directly. Instead, you can use the NSDate compare method, for example:
days.sort({ $0.date.compare($1.date) == NSComparisonResult.OrderedAscending })
Alternatively, you can extend NSDate to implement the Comparable protocol Comparable that it can be compared with < (and <= , > , >= , == ):
public func <(a: NSDate, b: NSDate) -> Bool { return a.compare(b) == NSComparisonResult.OrderedAscending } public func ==(a: NSDate, b: NSDate) -> Bool { return a.compare(b) == NSComparisonResult.OrderedSame } extension NSDate: Comparable { }
Note. You only need to implement < and == and shown above, then the rest of the operators <= , > , etc. will be provided by the standard library.
At the same time, your original sort function should work fine:
days.sort({ $0.date < $1.date })
Mike S Oct 26 '14 at 8:31 on 2014-10-26 20:31
source share