Advertisement
Larme

Untitled

Apr 30th, 2021
1,740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.83 KB | None | 0 0
  1. let dates: [Date] = []
  2. let hourComponents = DateComponents(hour: 10, minute: 0) //Here provide a better way
  3. func hourComponentsMatches(date: Date) -> Bool {
  4.     let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: date)
  5.     return hourComponents.hour == dateComponents.hour && hourComponents.minute == dateComponents.minute
  6. }
  7.  
  8. let sortedDates = dates.sorted(by: { first, second in
  9.     switch (hourComponentsMatches(date: first), hourComponentsMatches(date: second)) {
  10.     case (true, true):
  11.         return first < second //If order for all the one with the correct order is wrong, return first > second
  12.     case (false, true):
  13.         return true //if order is wrong, try by returning here false and on the next one true
  14.     case (true, false):
  15.         return false
  16.     default:
  17.         return false
  18.     }
  19. })
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement