joeyhal
joeyhal

Reputation: 13

Sort TableView Cells Alphabetically using Swift

I am using firebase to load data into tableview cells, and here is the structure of my data.

struct postStruct {
let title : String!
let author : String!
let bookRefCode : String!
let imageDownloadString : String!
let status : String!
let reserved : String!
let category : String!
let dueDate : String!
}

Now i need to sort the tableview cells holding the data alphabetically, with the titles starting with A at the top, and Z at the bottom.

class DirectoryTableView: UITableViewController {

var posts = [postStruct]()

override func viewDidLoad() {

let databaseRef = Database.database().reference()

databaseRef.child("Books").queryOrderedByKey().observe(.childAdded, with: {
    snapshot in

    var snapshotValue = snapshot.value as? NSDictionary
    let title = snapshotValue!["title"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let author = snapshotValue!["author"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let bookRefCode = snapshotValue!["bookRefCode"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let status = snapshotValue!["status"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let reserved = snapshotValue!["reserved"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let category = snapshotValue!["category"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let dueDate = snapshotValue!["dueDate"] as? String
    snapshotValue = snapshot.value as? NSDictionary


    self.posts.insert(postStruct(title: title, author: author, bookRefCode: bookRefCode, status: status, reserved: reserved, category: category, dueDate: dueDate) , at: 0)
    self.tableView.reloadData()


})



 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return posts.count

 }




  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var cell = tableView.dequeueReusableCell(withIdentifier: "cell")

let databaseRef = Database.database().reference()

let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].title

let label2 = cell?.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].author

let label3 = cell?.viewWithTag(3) as! UILabel
label3.text = posts[indexPath.row].bookRefCode

let label4 = cell?.viewWithTag(4) as! UILabel
label4.text = posts[indexPath.row].status

let label5 = cell?.viewWithTag(5) as! UILabel
label5.text = posts[indexPath.row].category

let image1 = cell?.viewWithTag(6) as! UILabel
image1.text = posts[indexPath.row].imageDownloadString

let label6 = cell?.viewWithTag(7) as! UILabel
label6.text = posts[indexPath.row].reserved

let label9 = cell?.viewWithTag(9) as! UILabel
label9.text = posts[indexPath.row].dueDate

return cell!

}

Any ideas, please help! I have tried to sort them with different methods, but I'm confused!

Upvotes: 1

Views: 7780

Answers (1)

Reinier Melian
Reinier Melian

Reputation: 20804

As I said in my comment you don't need to sort your cells you need sort your datarsource array instead, to do that add this line self.posts.sort(by: {$0.title < $1.title}) below your self.posts.insert(postStruct(title: title, author: author, bookRefCode: bookRefCode, status: status, reserved: reserved, category: category, dueDate: dueDate) , at: 0) adding this line your posts array will keep ordered and every time you add another post will be ordered again

Your viewDidLoad full code with the sort

override func viewDidLoad() {

let databaseRef = Database.database().reference()

databaseRef.child("Books").queryOrderedByKey().observe(.childAdded, with: {
    snapshot in

    var snapshotValue = snapshot.value as? NSDictionary
    let title = snapshotValue!["title"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let author = snapshotValue!["author"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let bookRefCode = snapshotValue!["bookRefCode"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let status = snapshotValue!["status"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let reserved = snapshotValue!["reserved"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let category = snapshotValue!["category"] as? String
    snapshotValue = snapshot.value as? NSDictionary

    let dueDate = snapshotValue!["dueDate"] as? String
    snapshotValue = snapshot.value as? NSDictionary


    self.posts.insert(postStruct(title: title, author: author, bookRefCode: bookRefCode, status: status, reserved: reserved, category: category, dueDate: dueDate) , at: 0)
    self.posts.sort(by: {$0.title < $1.title})
    self.tableView.reloadData()


})

Upvotes: 8

Related Questions