How to reorder elements (NSString) of an NSArray alphabetically?
NSSortDescriptor is overkill to sort an array whose elements are just NSString instances.
NSArray *sortedArray = [unsortedArray sortedArrayUsingSelector: @selector(compare:)];
If, instead of a new array instantiated with its elements sorted, you want to have the elements of an NSMutableArray instance reordered, there's a method for that:
[unsortedMutableArray sortUsingSelector: @selector(compare:)];
Use an NSSortDescriptor instance when you have a large object managing the array for you, like an NSArrayController or an NSTableView. If that's the case, and the elements are just instances of NSString, @iHS's answer would be correct.
you can use sortDescriptor
NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"yourKey" ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease];
NSArray * sortedArray =
[yourArray sortedArrayUsingDescriptors:descriptor];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With