How to reverse order of items from Firebase

前端 未结 2 387
花落未央
花落未央 2021-01-16 15:56

How can I reverse the order of this list in from Firebase? I want the last item to be the first. I tried to use queryOrdered, but that did not make a difference

相关标签:
2条回答
  • 2021-01-16 16:39

    As @Frank says, you need to sort your results client-side.

    You can either sort the array with something like this

    let sorted = self.RquestInfoArray.sorted(by: { (r1: RequestInfoType, r2: RequestInfoType) -> Bool in
                return r1.dateAmount > r2.dateAmount
    })
    

    but if you really just want to reverse the array, probably simpler just to add your new elements at the start instead of the end

    self.RquestInfoArray.insert(contentsOf: array, at: 0)
    
    0 讨论(0)
  • 2021-01-16 16:48

    One trick that I'm using is to add a field to my object that stores the time-stamp as reversed value. The value of this field I compute when I persist the object and is reversedDateCreated = timeStamp * -1 Now you can use default Firebase orderBy methods in order to sort and paginate by reversedDateCreated field

    0 讨论(0)
提交回复
热议问题