Reputation: 29896
I use this sort descriptor
NSArray *sortedObjects = [self.array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES], nil]];
The result is 10 and 11 are sorted alongside 1 and placed before 2.
index is 1/12
index is 10/12
index is 11/12
index is 2/12
index is 3/12
index is 4/12
index is 5/12
index is 8/12
index is 9/12
How can this be avoided?
Upvotes: 1
Views: 138
Reputation: 22006
I don't know of that class are the items that you are printing, presumably strings. This happens because the '/' character has an higher value than '0'.
You have to use a custom method or block to make the comparison. So initialise also the selector, not only the key:
NSSortDescriptor* sd= [[NSSortDescriptor alloc]initWithKey: @"index" ascending: YES selector: @selector(customCompare:)];
NSArray *sortedObjects = [self.array sortedArrayUsingDescriptors: @[ sd ] ];
In your customCompare: method you should compare the objects.
Upvotes: 0
Reputation: 112873
Use your own comparison with
- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr
or
another sort that allows a custom comparator.
Then split up the days and compare the month and then the day.
Upvotes: 1