I have two arrays, they are of two different objects, and both contain an ID field. What I need to do is display them in order in a table view controller. They share the sam
Alternatively to Zoff Dino answer if you do not want to burden TypeA
and TypeB
classes with HasID
protocol then you can define extension to these classes in your view controller:
class TypeA {
var ID: Int
init(_ id: Int) {
self.ID = id
}
}
class TypeB {
var ID: Int
init(_ id: Int) {
self.ID = id
}
}
protocol HasID {
var ID: Int { get }
}
// place this in your view controller
extension TypeA: HasID {
}
extension TypeB: HasID {
}
var arrayA = [TypeA(1), TypeA(3), TypeA(5)]
var arrayB = [TypeB(2), TypeB(4)]
let sortedArray = (arrayA.map { $0 as HasID } + arrayB.map { $0 as HasID })
.sort { $0.ID < $1.ID }
Put all common properties into a protocol, the build and sort an array of that common protocol:
protocol HasID {
var id: Int { get }
}
class TypeA : HasID, CustomStringConvertible {
var id: Int
init(_ id: Int) {
self.id = id
}
var description : String {
return ("TypeA(\(self.id))")
}
}
class TypeB : HasID, CustomStringConvertible {
var id: Int
init(_ id: Int) {
self.id = id
}
var description : String {
return ("TypeB(\(self.id))")
}
}
let typeA = [TypeA(1), TypeA(2), TypeA(5), TypeA(6)]
let typeB = [TypeB(3), TypeB(4), TypeB(7)]
let result: [HasID] = (typeA + typeB).sorted { $0.id < $1.id }
print(result)
[TypeA(1), TypeA(2), TypeB(3), TypeB(4), TypeA(5), TypeA(6), TypeB(7)]
You can do this like so:
class TypeA {
var ID: Int
init(_ id: Int) {
self.ID = id
}
}
class TypeB {
var ID: Int
init(_ id: Int) {
self.ID = id
}
}
struct Wrap {
var ID: Int {
return a?.ID ?? b?.ID ?? 0
}
let a: TypeA?
let b: TypeB?
}
var arrayA = [TypeA(1), TypeA(3), TypeA(5)]
var arrayB = [TypeB(2), TypeB(4)]
let sortedArray = (arrayA.map { Wrap(a: $0, b: nil) } + arrayB.map { Wrap(a: nil, b: $0)})
.sorted { $0.ID < $1.ID }
When row is selected you can determine object with:
if let a = sortedArray[index].a {
// TypeA row selected
} else if let b = sortedArray[index].b {
// TypeB row selected
}