I need to sort my tableview data by the id in ObjectBulletin. I have not been able to figure out how to apply a sort on the id and get the data reflect correctly.
My struct
struct BulletinHelper: Codable {
let limit: Int?
let offset: Int?
let objects: [ObjectBulletin]?
let totalCount: Int?
enum CodingKeys: String, CodingKey {
case limit = "limit"
case offset = "offset"
case objects = "objects"
case totalCount = "total_count"
}
}
struct ObjectBulletin: Codable {
let id: Int?
let portalID: Int?
let name: String?
let size: Int?
let height: JSONNullBulletin?
let width: JSONNullBulletin?
let encoding: JSONNullBulletin?
let type: String?
let objectExtension: String?
let cloudKey: String?
let s3URL: String?
let friendlyURL: String?
let altKey: String?
let altKeyHash: String?
let title: String?
let meta: MetaBulletin?
let created: Int?
let updated: Int?
let deletedAt: Int?
let folderID: Int?
let hidden: Bool?
let cloudKeyHash: String?
let archived: Bool?
let altURL: String?
let url: String?
enum CodingKeys: String, CodingKey {
case id = "id"
case portalID = "portal_id"
case name = "name"
case size = "size"
case height = "height"
case width = "width"
case encoding = "encoding"
case type = "type"
case objectExtension = "extension"
case cloudKey = "cloud_key"
case s3URL = "s3_url"
case friendlyURL = "friendly_url"
case altKey = "alt_key"
case altKeyHash = "alt_key_hash"
case title = "title"
case meta = "meta"
case created = "created"
case updated = "updated"
case deletedAt = "deleted_at"
case folderID = "folder_id"
case hidden = "hidden"
case cloudKeyHash = "cloud_key_hash"
case archived = "archived"
case altURL = "alt_url"
case url = "url"
}
}
Table
var bulletins: BulletinHelper?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "bullentinCell", for: indexPath)
let files = bulletins?.objects![indexPath.row]
cell.textLabel?.text = files?.name
cell.detailTextLabel?.text = "Weekly Bulletin"
return cell
}
My attempt at sorting
Which does give me the ids sorted but I need the related data to be sorted with it.
let mysort: [Int] = [(bulletins?.objects![indexPath.row].id)!]
let sortedlist = mysort.sorted()
print(sortedlist)