Reputation: 505
I have some data in firebase that I am trying to organize by timestamp. I have incorporated this code into cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let seconds = post["pub_time"] as? Double {
let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let formating = timeStampDate as Date
let timeAgo = timeAgoSinceDate(formating)
cell.Time.text = timeAgo
posts.sort(by: { $0.timeStampDate.compare($1.timeStampDate) == .orderedAscending })
}
But on the posts.sort(by: { $0.timeStampDate.compare($1.timeStampDate) == .orderedAscending })
I keep getting the error expression type bool is ambiguous without more context and I am not sure what I am doing wrong
This is more or less what my view controller looks like
var posts = NSMutableArray()
func loadData(){
Database.database().reference().child("main").child("posts").queryOrdered(byChild: "pub_time").observeSingleEvent(of: .value, with: { snapshot in
if let postsDictionary = snapshot .value as? [String: AnyObject] {
for post in postsDictionary {
posts.add(post.value)
}
self.TableView.reloadData()
}
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
let cell = tableView.dequeueReusableCell(withIdentifier: "Cells", for: indexPath) as! FeedTableViewCell
//Configure the cell
let post = posts[indexPath.row] as! [String: AnyObject]
cell.Title.text = post["title"] as? String
if let seconds = post["pub_time"] as? Double {
let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let formating = timeStampDate as Date
let timeAgo = timeAgoSinceDate(formating)
cell.Time.text = timeAgo
}
return cell
}
Upvotes: 0
Views: 1059
Reputation: 35659
Here's how you sort an array of objects in place; acending is the default order.
Assuming you have a PostClass like this
class PostClass {
var timeStampDate = "" //assume the timestamps are yyyyMMddhhmmss strings
var post = ""
}
and they are stored in a posts array
var posts = [PostClass]()
the code to sort the posts object within the array is:
posts.sort(by: {$0.timeStampDate < $1.timeStampDate })
that being said, you may want to consider letting Firebase do the heavy lifting and ordering the data for you so it's presented in the snapshot in the correct order.
Suppose you have a Firebase structure
posts
post_0
post: "A post about some stuff"
timestamp: "2"
post_1
post: "Posting about how to post"
timestamp: "3"
post_2
post: "The post for you"
timestamp: "1"
and we want to load our posts, ordering by the timestamp
let postsRef = self.ref.child("posts")
let postQuery = postsRef.queryOrdered(byChild: "timestamp")
postQuery.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let snap = child as! DataSnapshot
let dict = snap.value as! [String: Any]
let post = dict["post"] as! String
let ts = dict["timestamp"] as! Int
print(ts, post)
}
})
and the output
1 The post for you
2 Posting about how to post
3 A post about some stuff
For this example, I used 1, 2, 3 as my timestamp but yyyyMMddhhmmss (20180618101000) format would work as well and provide human readable, proper ordering.
Upvotes: 1