怎么对包含自定义对象的NSMutableArray排序
2013-06-05 12:58
387 查看
Compare method
Either you implement a compare-method for your object:- (NSComparisonResult)compare:(Person *)otherObject { return [self.birthDate compare:otherObject.birthDate]; } NSArray *sortedArray; sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];
NSSortDescriptor (better)
or usually even better:NSSortDescriptor *sortDescriptor; sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate" ascending:YES] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSArray *sortedArray; sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];You can easily sort by multiple keys by adding more than one to the array. Using custom comparator-methods is possible as well. Have a look at thedocumentation.
Blocks (shiny!)
There's also the possibility of sorting with a block since Mac OS X 10.6 and iOS 4:NSArray *sortedArray; sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { NSDate *first = [(Person*)a birthDate]; NSDate *second = [(Person*)b birthDate]; return [first compare:second]; }];For this particular example I'm assuming that the objects in your array have a 'position' method, which returns an
NSInteger.
NSArray *arrayToSort = where ever you get the array from... ;NSComparisonResult (^sortBlock)(id, id) = ^(id obj1, id obj2) {if ([obj1 position] > [obj2 position]) {return (NSComparisonResult)NSOrderedDescending;}if ([obj1 position] < [obj2 position]) {return (NSComparisonResult)NSOrderedAscending;}return (NSComparisonResult)NSOrderedSame;};NSArray *sorted = [arrayToSort sortedArrayUsingComparator:sortBlock];Note: the "sorted" array will be autoreleased.[/code]
If this 'position' is in NSDictory.
NSComparisonResult (^sortBlock)(id, id) = ^(id obj1, id obj2) { NSInteger p1= [((NSString*) [obj1 objectForKey:@"position"]) integerValue]; NSInteger p2= [((NSString*) [obj2 objectForKey:@"position"]) integerValue]; if (p1 > p2) { return (NSComparisonResult)NSOrderedDescending; } if (p1 < p2) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame; };
相关文章推荐
- 对NSMutableArray中的NSDictionary对象自定义排序
- OBjective-C:在可变数组NSMutableArray中添加相同对象后,进行自定义的排序方式输出
- NSMutableArray复杂对象排序
- NSMutableArray复杂对象排序
- 【已解决】iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- int,float怎么保存在NSMutableArray,这样的COCOA对象里面
- iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- NSMutableArray 复杂 对象 排序
- NSMutableArray中各种自定义排序方法
- NSArray(不可变数组)、NSMutableArray(可变数组)、数组排序、NSNumber(多态:数值和对象互转)、NSValue(将结构体转换成对象)
- NSMutableArray 排序
- ios数组排序NSMut 4000 ableArray *temp = [NSMutableArray arrayWithObjects:@"5",@"1",@"4",@"2",nil];
- 怎么用NSMutableArray存储CGPoint,CGSize,CGRect,CGAffineTransform,UIEdgeInsets,UIOffset
- NSMutableArray的双条件或多条件排序
- 0003-¥¥¥¥可变数组按条件排序sort NSMutableArray
- OC学习之道:OC对象,字符串和数组的使用方法NSString,NSMutableString ,NSArray,NSMutableArray
- NSArray/NSMutableArray创建,获取,遍历,排序 - iOS
- iPhone开发笔记[9/50]:NSMutableArray中的自动释放对象让我郁闷了一整天