NSArray与NSMutableArray

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

99封情书 提交于 2019-12-10 15:35:32
方法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