您好我想在两个属性上对数组进行排序。我怎么能在swift中做到这一点?
这就是我的代码现在的样子。
import Foundation
class AutosuggestModel {
var type = ""
var count = 0
init(type: String, count: Int) {
self.type = type
self.count = count
}
}
let array = [AutosuggestModel(type:"brand",count:10),AutosuggestModel(type:"brand",count:7),AutosuggestModel(type:"model",count:300),AutosuggestModel(type:"model",count:500),AutosuggestModel(type:"brand",count:50)]
排序还应包括对象的类型。如果类型是“品牌”,那么它的排序应该高于类型模型,也与count属性有关。所以我的例子的正确顺序应该是:
[AutosuggestModel(type:"brand",count:50),AutosuggestModel(type:"brand",count:10),AutosuggestModel(type:"brand",count:7),AutosuggestModel(type:"model",count:500),AutosuggestModel(type:"model",count:300)]
我的排序现在看起来像这样:
let ordered = array.sorted(by: {$0.count > $1.count})