ios - Parse.com Framework Sort Date Next Birthday -


need sort on ios date using parse.com need order them based on next birthday specific person. tried add day , month current or next year , helped manual process, recommendations in automated way.

as side note want implement in swift

thanks in advance.

the exact code depend on personal setup (i can imagine you've made subclass of pfobject or similar), try experiment following code. i've made function called filterfriendsafterbirthdays might useful reading post.

// //  viewcontroller.swift //  parsefun // //  created stefan veis pennerup on 20/06/15. //  copyright (c) 2015 kumuluzz. rights reserved. //  import uikit  class viewcontroller: uiviewcontroller {      override func viewdidload() {         createdummydata()         queryforfriends() {             println("unsorted birthdays: \($0)")             println("sorted birthdays: \(self.filterfriendsafterbirthdays($0))")             println("closets upcoming birthday: \(self.filterfriendsbasedonclosetsupcomingbirthday($0))")         }     }      func createdummydata() {         var = 0; < 25; i++ {             let myfriend = pfobject(classname: "friends")             let interval = double(arc4random_uniform(uint32.max))             myfriend["birthday"] = nsdate(timeintervalsince1970: interval)             myfriend.saveinbackground()         }     }      func queryforfriends(completionhandler: ([pfobject]) -> ()) {         let friendsquery = pfquery(classname: "friends")         friendsquery.findobjectsinbackgroundwithblock { (result, error) in             if (error != nil) { return }             let pfarray = result as! [pfobject]             completionhandler(pfarray)         }     }      func filterfriendsafterbirthdays(friends: [pfobject]) -> [pfobject] {         return friends.sorted {             return ($0["birthday"] as! nsdate).compare($1["birthday"] as! nsdate) == .orderedascending         }     }      func filterfriendsbasedonclosetsupcomingbirthday(friends: [pfobject]) -> [pfobject] {         let cal = nscalendar.currentcalendar()          // specifies day unit         let dayunit: nscalendarunit = .calendarunitday          // gets todays year         let today = nsdate()         let yearunit: nscalendarunit = .calendarunityear         let yeartoday = cal.components(yearunit, fromdate: today)          // combined days , year units         let combinedunits: nscalendarunit = .calendarunityear | .calendarunitday          return friends.sorted {             // gets birthday components since today             // uses year unit ensure day between -365 , 365             let birth1components = cal.components(combinedunits, fromdate: today, todate: ($0["birthday"] as! nsdate), options: nil)             let birth2components = cal.components(combinedunits, fromdate: today, todate: ($1["birthday"] as! nsdate), options: nil)              // updates days positive integer             if (birth1components.day < 0) { birth1components.day += 365 }             if (birth2components.day < 0) { birth2components.day += 365 }              return birth1components.day <= birth2components.day         }     } } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -