I am sorting an NSMutableArray on my field “date” but it does not take into account the time. If the date is the same, the time is still sorted random. What am I doing wrong? The field “date” is of the type NSDate.
NSSortDescriptor *dateDescriptor =
[[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:dateDescriptor, nil];
[self.player1ScoreT sortedArrayUsingDescriptors:descriptors];
My output is:
2012-11-25 11:01:00 +0000
2012-11-25 11:00:56 +0000
2012-11-25 11:00:54 +0000
2012-11-25 11:01:03 +0000
Strange…..
The logic you’re using to sort is correct, I’ve just tried it and it works fine.
I assume it’s because of
[self.player1ScoreT sortedArrayUsingDescriptors:descriptors];, this will not change the array held inself.player1ScoreT, rather, it produces a new array. This is because you’re using a selector that will not mutate the original array (NSArrayitself is not mutable by design,NSMutableArrayis mutable).As such, you’ll want to use this:
This will reassign the array within
self.player1ScoreTto be the new sorted array, produced bysortedArrayUsingDescriptors:, and the old unsorted one will be discarded.