If I have a structure ...
struct MyStruct {
let number: Int
}
and I have an array of them ...
var array = [MyStruct]()
// populate array with MyStructs
Then I can do this to get the most number...
var maxNumber = 0
for tempStruct in array where tempStruct.number > maxNumber {
maxNumber = tempStruct.number
}
However, I can not use ...
let maxStruct = array.maxElement()
because it is MyStructnot comparable. I could make it comparable, but then I might also have a date stamp that I want to compare, so making Comparable is not perfect.
Is there any other way to make this more elegant?
....
I just thought I could do it too ...
let maxStruct = array.sort{$0.number > $1.number}.first()
But it will take more time. I'm not sure which sorting method he uses, but it will be n log(n), whereas my original method will be simple n.