开发者

Sort Cells by Using NSDictionaries from an NSMutableArray

I have NSDictionaries stored in an NSMutableArray. Each NSDictionary has a NSNumber inside of it. So right now, my tableview will show the cells in the way they are ordered in the array. What I want to do is reorder the cells from highest NSNumber to lowest NSNumber, so how would I do this with all the NSDictionaries in the NSMutableArray. I will probably need a for loop or something like that but I am not sure how to do it.

Also I have NSDate's (non-formatted) in my NSDictionaries, so how would I sort the NSDictionaries based upon the n开发者_如何学编程ewest dates inside them?

Thanks!


You need to use the sortedArrayUsingSelector method of NSMutableArray and provide a sorting function, like this:

NSMutableArray *yourSortedArray;
yourSortedArray = [originalArray sortedArrayUsingSelector:@selector(yourCompareFunction:)];

...

- (NSComparisonResult)yourCompareFunction:(NSDictionary *)otherObject {
   return [[self objectForKey:@"number_key"] compare:[otherObject objectForKey:@"number_key"]];
}

I didn't tested this but that's the idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜