2017-04-18 81 views
0

我正在使用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() 
     } 
    } 
}) 
+1

你能告訴created_at'的'採樣值,因爲它是'String'類型,所以你需要向我們展示了最新 –

+1

可能的複製格式[Swift如何通過屬性值對自定義對象數組進行排序](http://stackoverflow.com/questions/24130026/swift-how-to-sort-array-of-custom-objects-by-property-value) –

回答

0

您只需使用

queryOrderedByChild檢索火力地堡已經排序的數據。

一個例子是:

ref.child(bottleID).queryOrderedByChild("created_at").queryEqualToValue(0).observe SingleEventOfType(.Value, withBlock: { snap in 
    print("snap \(snap)") 
    expectation.fulfill() 
})