如何在Swift中使用querySnapshot将Firebase顺序升序排列 [英] How to sort firebase order to Ascending using querySnapshot in swift

查看:123
本文介绍了如何在Swift中使用querySnapshot将Firebase顺序升序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,正在使用Firebase.我正在获取数据,但顺序不正确.我想按升序排序. 我的代码是这样的

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()
           }
       }
    }

我想根据文档过滤它,就像图像中1后面是10一样,我很快就得到了相同的数据.但我想对文档进行排序,这样我将收到1,2,3 ...

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!

推荐答案

您是否尝试过对集合的sort方法? 例如:

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

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

这篇关于如何在Swift中使用querySnapshot将Firebase顺序升序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆