If I have a struct...
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 maximum number...
var maxNumber = 0
for tempStruct in array where tempStruct.number > maxNumber {
maxNumber = tempStruct.number
}
However, I can't use...
let maxStruct = array.maxElement()
because MyStruct is not comparable. I could make it comparable but then I might also have a date stamp that I want to compare by so making it Comparable isn't ideal.
Is there another way I could do this that is more elegant?
....
I just thought, I could also do this...
let maxStruct = array.sort{$0.number > $1.number}.first()
But this will take more time. I'm not sure which sort method it uses but it'll prob be n log(n) whereas my initial method will just be n.