简体   繁体   English

如何在Swift中对对象数组进行排序?

[英]How to sort an array of object in Swift?

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? 有什么建议么?

sortsorted

let sortedIntervals = intervals.sort { (a, b) in return a.startTime < b.startTime}

You are doing it wrong. 你做错了。 Use sort instead of sorted . 使用sort而不是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. 有关更多信息,请参见Apple的Doc

Hope this helps.. :) 希望这可以帮助.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM