Hello I want to sort an array over two properties. How can I do this in swift?
This is how my code looks right now.
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)]
The sorting should also include the type of the object. If the type is "brand" than it should be ordered higher than type model and also in relation to the count property. So the right order for my example should be this:
[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)]
My sorting looks now like this:
let ordered = array.sorted(by: {$0.count > $1.count})