方法1:使用
Compare method
- (NSComparisonResult)compare:(Person *)otherObject {
return [self.birthDate compare:otherObject.birthDate];
}
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];
方法2:
NSSortDescriptor (better)
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"
ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];
sortedArrayUsingDescriptors
sortedArrayUsingDescriptors返回的是NSArray * ,如果对NSMutableArray进行排序,需要
mutableSortedArray=(NSMutableArray *)[mArray sortedArrayUsingDescriptors:xxx];
来源:oschina
链接:https://my.oschina.net/u/1167726/blog/142382