How to sort an NSMutableArray with custom objects in it?

碰到了个问题,谷歌了一下,觉得这个回答,回答的真好,因此在这里记录一下。

http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it

How to sort an NSMutableArray with custom objects in it?


What I want to do seems pretty simple, but I can't find any answers on the web. I have anNSMutableArray of objects, let's say they are 'Person' objects. I want to sort the NSMutableArrayby Person.birthDate which is an NSDate.

I think it has something to do with this method:

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

In Java I would make my object implement Comparable, or use Collections.sort with an inline custom comparator...how on earth do you do this in Objective-C?


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 the documentation.

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];
}];
share | improve this answer
 
24  
The first example has a bug: You compare the birthDate instance variable in one object with the other object itself, rather than its birthDate variable. –  Martin Gjaldbaek  May 17 '11 at 8:52
38  
@Martin: Thanks! Funny that nobody else noticed before I got 75 upvotes for it. –  Georg Schölly  May 17 '11 at 11:18
28  
Because this is the accepted answer, and therefore probably considered definitive by most users, it might be helpful to add a 3rd, block-based example so that users will be aware it exists too. –  orange80  Dec 2 '11 at 20:30
3  
@orange80: I tried that. I don't own a Mac any more, so it would be nice if you could look at the code. –  Georg Schölly  Dec 4 '11 at 20:25
3  
@GeorgSchölly: You got it exactly right. –  orange80  Dec 5 '11 at 0:07

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值