Firebase按值分类排序数组
问题描述:
我正在使用Firebase。在我的应用程序中,我通过传入bottleID并从快照中获取该值的详细信息来获取子值。然后,我将这些细节分配给MyCollection_Class
的一个对象,并将其添加到数组中。获取每一个瓶子值后,我想在重新加载表格视图之前使用created_at
标签对该数组进行排序。请告诉我如何通过特定的实例变量对对象数组进行排序。Firebase按值分类排序数组
let Collection = MyCollection_Class()
FireBaseConstants.AUCTIONS_REF.child(bottleID).observeSingleEvent(of: .value, with: { (snap) in
if !(snap.value is NSNull) {
Collection.id = bottle_dict["id"] as? String
Collection.item_number = bottle_dict["item_number"] as? Int
Collection.created_at = bottle_dict["created_at"] as? String
if !(self.MyCollectionsIDArr.contains(Collection.id! as String)) {
self.MyCollectionsArr.append(Collection)
self.MyCollectionsIDArr.append(Collection.id!)
// I want to sort the MyCollectionsArr using created_at here
self.tbl_Latest.reloadData()
}
}
})
答
您只需使用
queryOrderedByChild检索火力地堡已经排序的数据。
一个例子是:
ref.child(bottleID).queryOrderedByChild("created_at").queryEqualToValue(0).observe SingleEventOfType(.Value, withBlock: { snap in
print("snap \(snap)")
expectation.fulfill()
})
你能告诉created_at'的'采样值,因为它是'String'类型,所以你需要向我们展示了最新 –
可能的复制格式[Swift如何通过属性值对自定义对象数组进行排序](http://stackoverflow.com/questions/24130026/swift-how-to-sort-array-of-custom-objects-by-property-value) –