I have the following code:
struct MyData {
var company = String();
var score:Int;
}
let data = [
MyData(company: "smiths", score: 4 ),
MyData(company: "lukes", score: 4),
MyData(company: "lukes", score: 9)
]
extension MyData: CustomStringConvertible {
var description: String {
return "(\(company), \(score))"
}
}
data.sorted { ($0.company, $1.score) < ($1.company, $0.score) }
print(data)
My output is:
[(smiths, 4), (lukes, 4), (lukes, 9)]
However, I want it to be the other way around:
[(lukes, 9), (lukes, 4), (smiths, 4)]
Can someone show me what it is I'm doing wrong?
sortedreturns a new array containing the sorted results, it's not a mutating function. It couldn't even work with a mutating function, sincedatais immutable.<to>.