How to sort a mutablensarray?
I made this code to sort an nsmutablearray but how to send back an NSMutablearray ? i need this because a add information later in my mutablearray.
-(NSArray*)trierTableau:(NSMutableArray*)ptableau champsFiltre:(NSString*) champs{
NSSortDescriptor *lastDescriptor =
[[[NSSo开发者_如何学编程rtDescriptor alloc]
initWithKey:champs
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray * descriptors = [NSArray arrayWithObjects:lastDescriptor, nil];
NSArray * sortedArray = [ptableau sortedArrayUsingDescriptors:descriptors];
return sortedArray;
}
If you want to sort your NSMutableArray
in-place, then you could call sortUsingDescriptors:
instead of sortedArrayUsingDescriptors:
, which returns a sorted copy of your array. sortUsingDescriptors:
will sort the existing NSMutableArray
.
e.g.
-(void)trierTableau:(NSMutableArray*)ptableau champsFiltre:(NSString*) champs {
NSSortDescriptor *lastDescriptor =
[[[NSSortDescriptor alloc]
initWithKey:champs
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray * descriptors = [NSArray arrayWithObjects:lastDescriptor, nil];
[ptableau sortUsingDescriptors:descriptors];
}
This way you don't have to return the NSMutableArray
either.
If you actually want a sorted copy of the array, then you'll need to create a new NSMutableArray
, use addObjectsFromArray:
to copy the elements over, and then use sortUsingDescriptors:
on the new array. But I think that sorting the existing NSMutableArray
is probably what you'll want.
精彩评论