Comparable
A subprotocol of Equatable is Comparable:
protocol Comparable: Equatable {
static func <(lhs: Self, rhs: Self) -> Bool static func <=(lhs: Self, rhs: Self) -> Bool static func >=(lhs: Self, rhs: Self) -> Bool static func >(lhs: Self, rhs: Self) -> Bool
}
In addition to the equality operator ==, Comparable requires you to overload the comparison operators <, <=, > and >= for your type. In practice, you’ll usually only provide <, as the standard library can implement <=, > and >= for you, using your implementations of == and <.
Make Record adopt Comparable as shown below:
extension Record: Comparable {
static func <(lhs: Record, rhs: Record) -> Bool { if lhs.wins == rhs.wins {
return lhs.losses > rhs.losses
}
return lhs.wins < rhs.wins
}
}
This implementation of < considers one record lesser than another record if the first record either has fewer wins than the second record, or an equal number of wins but a greater number of losses.