how to sort NSMutableArray
Possible Duplicate:
How to sort an NSMutableArray with custom objects in it?
Having trouble with something basic (I think).
I have an NSMutable array that I populate with "workout" objects. The "startDate" method returns an NSDate - and I'd like to sort the array off this.
The NSSortDescriptor *sortByDate line terminates due to an uncaught exception. What's wrong here?
NSMutableArray *workouts;
workouts = [[[Workout findByCriteria:@"where state = %@ order by pk desc",
[NSNumber numberWithInt:WorkoutStateComplete]] mutableCopy] autorelease];
NSSortDescriptor *sortByDate = [NSSortDescriptor
sortDescriptorWithKey:@"startDate" ascending:NO];
[workouts sortUsingDescriptors:[NSArray arrayWithObject:sortByDate]];
Make sure none of the objects in the workouts array have startDate
set to nil or return nil. Also make sure that all objects in the array respond to the startDate
selector.
上一篇: 将NSMutableArray与其中的对象进行排序
下一篇: 如何排序NSMutableArray