arrays - How to order PFObjects based on its creation date? -


i have user comments stored in database (parse-server) like display on viewcontroller's viewdidload(). can pull comment objects follows: super.viewdidload()

    func query(){         let commentsquery = pfquery(classname: "comments")         commentsquery.wherekey("objectid", equalto: detaildisclosurekey)         commentsquery.findobjectsinbackground { (objectss, error) in             if let objects = objectss{                 if objects.count == 1{                     object in objects{                         self.unorderedcomments.append(object)                     }                 }             }         }      } 

this query dumps of of comments in unorederedcomments array. each comment added database createdat property automatically being added relating exact time of creation. property string (as example) form: "2017-08-13t19:31:47.776z" (the z @ end @ end of every string... not sure why there constant). now, each new comment added in order top of database , queried result should in order regardless. however, make sure of reordering if necessary. general thought process use .sorted, cannot figure out how apply situation

func ordercomments(unordercomments: [pfobject]) -> [pfobject]{     let orderedeventcomments = unorderedeventcomments.sorted(by: { (<#pfobject#>, <#pfobject#>) -> bool in             //code         }) } 

this generic set cannot, despite looking several examples online figure out put in <#pfobject#>'s , in //code. want order them based on "createdat" property not achieved via dot notation , instead requires pfobject["createdat"] , using notation keeps leading error. feel though may need set custom predicate not know how this.

i in same situation, did first create array of structs data downloaded turned string createdat date, used function:

dataarrayordered = unorderedarray.sorted(by: { $0.date.compare($1.date) == .orderedascending}) 

(.date being stored date inside array of strcuts)

try code, notice assumed have variable name called ["comments"] inside parse database, replace if necessary. also, realised createdat it's in date format, there no need change string date, chek if works same you, if doesn't refer this: swift convert string date.

    struct comment {         var date = date()         var comment = string()     }      var unorderedcomments: [comment] = []     var orderedcomments = [comment]()       override func viewdidload() {     super.viewdidload()          query()   }       func query(){         let commentsquery = pfquery(classname: "comments")         commentsquery.findobjectsinbackground { (objectss, error) in              if let objects = objectss{                 if objects.count >= 1{                     object in objects{                         let newelement = comment(date: object.createdat!, comment: object["comments"] as! string)                         self.unorderedcomments.append(newelement)                         print(self.unorderedcomments)                     }                 }                 self.orderedcomments = self.unorderedcomments.sorted(by: { $0.date.compare($1.date) == .orderedascending})                 print(self.orderedcomments)             }         }     } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -