Reputation: 979
I am new in swift and I am working with firebase. I am getting the data but not in the correct order. I want to sort it into ascending order. My code is like this
func getBuzz(){
db.collection("tablename").getDocuments { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
guard let docs = querySnapshot?.documents else {
return
}
print(docs)
for document in docs {
self.arrdescriptionbuzz.append(document["description"] as Any)
self.arrimagebuzz.append(document["image"] as Any)
self.arrnumericdigitbuzz.append(document["numeric_digit"] as Any)
self.arrtitlebuzz.append(document["title"] as Any)
self.arrlinkbuzz.append(document["link"] as Any)
}
for (index, element) in self.arrimagebuzz.enumerated() {
print("get Index \(index): getString \(element)")
let url = NSURL(string:element as! String)
if !(url?.absoluteString == "") {
let data = NSData(contentsOf: url! as URL) //make sure your image in this url does exist, otherwise unwrap in a if let check
if !(data?.isEmpty ?? true){
self.arrimagenewbuzz.append(UIImage(data: data! as Data) ?? UIImage (named: "Default.jpeg")!)
}else{
self.arrimagenewbuzz.append(UIImage (named: "Default.jpeg")!)
}
}
}
print("Data = ", self.arrimagenewbuzz.count)
print("Image = ",self.arrimagebuzz.count)
print("Title = ",self.arrtitlebuzz.count)
print("Description = ",self.arrdescriptionbuzz.count)
self.BuzzCollectionView.reloadData()
}
}
}
I want to filter it according to document like as in the image there is 10 after 1 and I am getting the same data in swift. But I want to sort document so i will receive 1,2,3...
How can I sort it in swift. Thanks in Advance!
Upvotes: 0
Views: 1473
Reputation: 697
Have you tried the sort
method on collections?
e.g.:
docs.sorted { $0["some_value"] as! Int > $1["some_value"] as! Int }
Upvotes: 1
Reputation: 600
Have you tried use filter
method? Adding the method in your db reference, will help you get the data sorted without sorting from front-end side of iOS
Depends which Database you're using for Firebase
Realtime DB: https://firebase.google.com/docs/database/ios/lists-of-data#sorting_and_filtering_data Firestore: https://firebase.google.com/docs/firestore/query-data/order-limit-data
Upvotes: 0