How to sort firebase order to Ascending using querySnapshot in swift

十年热恋 提交于 2020-05-17 06:56:27

问题


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!


回答1:


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




回答2:


Have you tried the sort method on collections? e.g.:

docs.sorted { $0["some_value"] as! Int > $1["some_value"] as! Int }


来源:https://stackoverflow.com/questions/61026927/how-to-sort-firebase-order-to-ascending-using-querysnapshot-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!