I'd like to write a method that takes either a Set
or Array
,
like a Collection
-conforming parameter, or something like that
The problem is that it appears that sorted(by:)
exists on the Set
and Array
level,
https://developer.apple.com/documentation/swift/set/2296160-sorted
https://developer.apple.com/documentation/swift/array/2296815-sorted
and isn't declared in any of the protocols that both of them conform to.
Here is my sort method:
class func sortArray(_ array:[Bom]) -> [Bom] {
or
class func sortArray(_ array:Set<Bom>) -> [Bom] {
return array.sorted {
if $0.prop1 == $1.prop1 {
if $0.prop2 == $1.prop2 {
if $0.prop3 == $1.prop3 {
return $0.prop4 ?? "" < $1.prop4 ?? ""
} else {
return $0.prop3 ?? "" < $1.prop3 ?? ""
}
} else {
return $0.prop2 ?? "" < $1.prop2 ?? ""
}
} else {
return $0.prop1 < $1.prop1
}
}
}
I can copy paste this method, and change the parameter type from [BOM]
to Set<Bom>
everything works, but now I have a literal copy-pasted function, with the only difference being the parameter type...
am I missing something simple here?