How to sort a fast array containing instances of a subclass of NSManagedObject by attribute value (date)

I am trying to sort an array as outlined in the accepted answer to this question , but I ran into a problem that Isuru mentions the answer in the comments on this question. Namely, the code that should sort the array by the attribute "date" of the object brings a compiler complaint "could not find the date of the participant"

Here is a subclass of NSManagedObject that describes the object:

import Foundation import CoreData @objc(Entry) class Entry: NSManagedObject { @NSManaged var date: NSDate @NSManaged var reflections: AnyObject @NSManaged var contactComment: NSSet @NSManaged var person: NSSet override func awakeFromInsert() { let now:NSDate = NSDate() self.date = now; } } 

And here is the code that is trying to sort the array:

 lazy var entries:[Entry] = { var days:[Entry] = self.managedObjectContext!.requestEntity("Entry")as [Entry] days.sort({$0.date < $1.date}) var today:Entry = days.last! println(today.date) return days }() 

Please note that in the second part of this code I can access and register the "date" property for one of the records, and the compiler has no problems with it.

Is my syntax for proper sorting? Is there any other problem with this code that I don't see?

+43
sorting arrays cocoa swift core-data
Oct 26 '14 at 20:21
source share
4 answers

This is partly due to the fact that the Swift compiler does not give you a useful error. The real problem is that NSDate cannot be compared to < directly. Instead, you can use the NSDate compare method, for example:

 days.sort({ $0.date.compare($1.date) == NSComparisonResult.OrderedAscending }) 

Alternatively, you can extend NSDate to implement the Comparable protocol Comparable that it can be compared with < (and <= , > , >= , == ):

 public func <(a: NSDate, b: NSDate) -> Bool { return a.compare(b) == NSComparisonResult.OrderedAscending } public func ==(a: NSDate, b: NSDate) -> Bool { return a.compare(b) == NSComparisonResult.OrderedSame } extension NSDate: Comparable { } 

Note. You only need to implement < and == and shown above, then the rest of the operators <= , > , etc. will be provided by the standard library.

At the same time, your original sort function should work fine:

 days.sort({ $0.date < $1.date }) 
+124
Oct 26 '14 at 8:31 on
source share

In Swift 3, dates are now directly comparable:

 let aDate = Date() let bDate = Date() if aDate < bDate { print("ok") } 

Old quick: An alternative would be to sort the timeIntervalSince1970 value of a date object that is directly comparable.

 days.sort({$0.date.timeIntervalSince1970 < $1.date.timeIntervalSince1970}) 
+16
Jan 14 '16 at
source share

In swift2.1 use these lines of code

 array.sortInPlace({ $0.date.compare($1.date) == NSComparisonResult.OrderedAscending }) 
+10
Jan 21 '16 at 9:10
source share

I have one dictionary whose keys are in date format, so I have to sort the dictionary by date. I copied all the keys to nsarray, then sorted this array using the following code.

 let keys : NSArray = stackedBarDataDict.allKeys // stackedBarDataDict is Data Dictionary let dataArray = NSMutableArray() for index in 0...(stackedBarDataDict.allKeys.count - 1) { let dateone = keys.objectAtIndex(index) let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" let date1 = dateFormatter.dateFromString(dateone as! String) dataArray.addObject(date1!) } let array : NSArray = (dataArray as NSArray) let sortedArray = array.sortedArrayUsingComparator { (obj1, obj2) -> NSComparisonResult in let p1 = obj1 as! NSDate let p2 = obj2 as! NSDate let result = p1.compare(p2) return result } print(sortedArray) 
0
Oct 13 '16 at 6:37
source share



All Articles