0

enter image description here

so im trying to see if there's a way to order the specific documents from most recent to oldest is there a way to do it from firestore? or do I have to code it ?

this is the code on how I get the specific document ids im just using UUID().string which generated one for me

func quoteMeVariables(){

    let quotemodel = quoteME()

    quotemodel.quoteMEid = UUID().uuidString
    quotemodel.author = authorTextfield.text
    quotemodel.QuoteText = quoteMeTextfield.text
    quotemodel.saveQuoteMetoFirebase(quotemodel) //


    hud.textLabel.text = "Your Quote Has Been Sent Successfully!"
    hud.show(in: self.view)
    hud.indicatorView = JGProgressHUDSuccessIndicatorView()
    hud.dismiss(afterDelay: 3.0)

    DispatchQueue.main.asyncAfter(deadline: .now() + 3.0){

        self.dismiss(animated: true, completion: nil)
    }

}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Rayisooo
  • 122
  • 8
  • 2
    You need to add a timestamp to be able to sort based on that – Akshay Jain May 01 '20 at 08:23
  • 1
    There are a number of solutions but what you're showing in your question is not an array. That being said, if you simply add a timestamp field to each document, they can be easily sorted. – Jay May 01 '20 at 19:58

1 Answers1

0

You can sort the array inside Firestore, but if you need the array sorted, when you fetch it, you need to do it in code. You need to add a timestamp, from the server, to your document.

Go look here, if you don't know how.

Then you can .order(by: timestamp) on your collection, to get it sorted by timestamp.

Skou
  • 131
  • 6
  • You cannot sort an array in Firestore. An array is a fixed sequence of fields that cannot be sorted by the server. The sequence is the sequence by index. This is the downside of arrays in NoSQL databased. – Jay May 01 '20 at 19:54