对NSArray与NSMutableArray按照其存储的对象的属性进行排序

方法1:使用

Compare method

- (NSComparisonResult)compare:(Person *)otherObject {
    return [self.birthDate compare:otherObject.birthDate];
}

NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];

方法2: spa

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]; code

相关文章
相关标签/搜索