1

enter image description here

I need to show these data in a table view, but they have been on the table view in mixing order. How can I put them in the same order as in the database. Please help, I will deeply appreciated.

First image shows my iPhone screen after running the project, second one is the database. I want them exact order as in the database.

9
  • Not clear how your data is sorted in Firebase DB — is it by key/UID or by priority? Commented Apr 13, 2017 at 1:46
  • Thank you for response, I have added the 2nd image to be more clear. Commented Apr 13, 2017 at 1:56
  • @muhcomen check my answer. If you have another questions - you are welcome Commented Apr 13, 2017 at 5:43
  • @muhcomen has it helped you? Commented Apr 13, 2017 at 13:48
  • Are you sure you want them displayed in the table in the same order shown in the screenshot? That's NOT the order it appears you wrote them to the database. i.e. period0, period1, period2 appears to be the order you wrote them, not period0, period1, period10... The period10 is coming after period1 because they are sorted via ASCII sorting and showing in ASCII order, not the order you wrote them. If that's the case then the accepted answer won't work. Commented Apr 14, 2017 at 17:26

1 Answer 1

2

I hope periods objects look like this:

struct PeriodItem {
    let key: String

    let periodEnd: String
    let periodName: String
    let periodStart: String

    let ref: FIRDatabaseReference?

    init(periodEnd: String, periodName: String, periodStart: String, key: String = "") {
        self.key = key

        self.periodEnd = periodEnd
        self.periodName = periodName
        self.periodStart = periodStart

        self.ref = nil
    }

    init(snapshot: FIRDataSnapshot) {
        key = snapshot.key

        let snapshotValue = snapshot.value as! [String: AnyObject]
        periodEnd = snapshotValue["periodEnd"] as! String
        periodName = snapshotValue["periodName"] as! String
        periodStart = snapshotValue["periodStart"] as! String

        ref = snapshot.ref
    }

    func toAnyObject() -> Any {
        return [
            "periodEnd": periodEnd,
            "periodName": periodName,
            "periodStart": periodStart,

            "key": key
        ]
    }
}

So

When you fill your array of Periods fully, just use sorting:

// periods - array of objects from firebase database
let yourTableViewPeriodsArray = periods.sorted(by: { $0.key < $1.key }) // maybe ">" instead of "<"

Then:

DispatchQueue.main.async {
    self.tableView.reloadData()
}

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

When I call print([PeriodItem].self) in viewDidLoad I get Array<PeriodItem> instead of the actual array. Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.