Firebase按字母顺序获取数据

时间:2019-05-10 00:35:12

标签: swift firebase firebase-realtime-database

如何按字母顺序对以下数据进行排序?因此,“ deviceRooms1,deviceRooms2,deviceRooms3,...”我希望对数据进行排序。

Firebase Data

@objc func fetchRooms()
        {
     let ref = Database.database().reference().child("0").child("Rooms")

            ref.observeSingleEvent(of: .value, with: { (snapshot) in
                //let value: NSDictionary = snapshot.value as! NSDictionary
                let dic = snapshot.value as! [String:String]
                self.tableRowss = Array(dic.values)
                self.myTableView.reloadData()

            })

    }

1 个答案:

答案 0 :(得分:2)

您可以尝试

let dic = snapshot.value as! [String:String]
let sortedKeys = Array(dic.keys).sorted(by:<)
self.tableRowss = sortedKeys.map { dic[$0]! }
self.myTableView.reloadData()