I have objec开发者_如何学JAVAts in my NSMUtableArray and I need to sort them in descending order based on the tax.taxName.Can any body suggest me how to do this.
Hope I get Quick response from you guys.
Thanks in advance. Monish.
If assume right, that the objects in your array are of a custom class, implement a method in that class:
- (NSComparisonResult)compareByTax:(id)other
{
return [[self valueForKeyPath: @"tax.taxName"] compare: [other valueForKeyPath: @"tax.taxName"]];
}
Then do the following:
[array sortUsingSelector: @selector(compareByTax:)];
Since iOS4 you can use comparators with this NSArray message:
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
you would write something like this:
NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id object1, id object2) { // return NSComparisonResult ... }]
Another possibility is to define a c function which is used as callback with this NSArray message:
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context
Take a look in the NSArray Documentation there are messages for sorting with selectors or descriptors also. It depends on what fits best for you.
精彩评论