對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

相關文章
相關標籤/搜索