0

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)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
MwcsMac
  • 6,810
  • 5
  • 32
  • 52

2 Answers2

1

Based on the response from @Kamran I was able to come up with this solution that worked as expected.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "bullentinCell", for: indexPath)
    let sortedlist = bulletins?.objects?.sorted(by: { ($0.id ?? 0) > ($1.id ?? 0) })
    let files = sortedlist![indexPath.row]
    cell.textLabel?.text = files.name
    cell.detailTextLabel?.text = "Weekly Bulletin"

    return cell
}
MwcsMac
  • 6,810
  • 5
  • 32
  • 52
0
struct BulletinHelper {
    var objects: [ObjectBulletin]?
}

struct ObjectBulletin {
    var name: String?
    var id: Int?
}

var objects = [ObjectBulletin(name: "name 10", id: 10),
           ObjectBulletin(name: "name 13", id: 13),
           ObjectBulletin(name: "name 21", id: 21),
           ObjectBulletin(name: "name 5", id: nil),
           ObjectBulletin(name: "name 7", id: 7),
           ObjectBulletin(name: "name 4", id: 4),
           ObjectBulletin(name: "name 121", id: 121),
           ObjectBulletin(name: "name 2", id: 2),
           ObjectBulletin(name: "name 1", id: 1)]

var bulletins: BulletinHelper? = BulletinHelper(objects: objects)

let sortedList = bulletins?.objects?.sorted(by: { $0.id ?? 0 < $1.id ?? 0 })
bulletins?.objects = sortedList
bulletins?.objects?.map { print($0) }

This should sort your array.

Cavan
  • 1