I am attempting to sort an NSArray that is populated with custom objects. Each object has a property startDateTime
that is of type NSDate.
The following code results in an array, sortedEventArray
, populated but not sorted. Am I going about this the completely wrong way or am I just missing something small?
NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"startDateTime"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
NSArray *sortedEventArray = [nodeEventArray sortedArrayUsingDescriptors:sortDescriptors];
Best Answer
Are you sure that the
startDateTime
instance variables of the node events are non-nil?If you don't have one already, you might add a (custom)
-description
method to your node event objects that does something like this:Then in your sorting code log the array before and after:
If the
startDateTime
's are allnil
, then the before and after arrays will have the same order (since the sorting operation will equate to sending all the-compare:
messages tonil
, which basically does nothing).