Here's a simple Swift class with 3 fields:
public class Cabbage: Comparable {
public let someString: String
public let someInt: Int
public let someDouble: Double
public init(_ someString: String, _ someInt: Int, _ someDouble: Double) {
self.someString = someString
self.someInt = someInt
self.someDouble = someDouble
}
public static func ==(lhs: Cabbage, rhs: Cabbage) -> Bool {
if lhs.someString == rhs.someString {
if lhs.someInt == rhs.someInt {
if lhs.someDouble == rhs.someDouble {
return true
}
}
}
return false
}
public static func <(lhs: Cabbage, rhs: Cabbage) -> Bool {
if lhs.someString < rhs.someString {
if lhs.someInt < rhs.someInt {
if lhs.someDouble < rhs.someDouble {
return true
}
}
}
return false
}
}
I think my first function, func ==(), is correct. We return true if and only if all the fields are equal.
But I do not think my logic is correct for func <().
For example if lhs.someString == rhs.someString, should I then be comparing lhs.someInt and rhs.someInt?
And if these two are equal, should I also be comparing lhs.someDouble and rhs.someDouble?
Any ideas or recommendations would be much appreciated.