码迷,mamicode.com
首页 > 编程语言 > 详细

NSArray排序方法

时间:2016-01-21 19:36:48      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

sortedArrayUsingComparator:

NSMutableArray *array = [NSMutableArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber numberWithInt:0], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber numberWithInt:5], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj2", [NSNumber numberWithInt:2], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj3", [NSNumber numberWithInt:3], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj1", [NSNumber numberWithInt:1], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj4", [NSNumber numberWithInt:4], nil], nil];

// NSArray *resultArray = [array sortedArrayUsingSelector:@selector(compare:)];

NSArray *resultArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

NSNumber *number1 = [[obj1 allKeys] objectAtIndex:0];
NSNumber *number2 = [[obj2 allKeys] objectAtIndex:0];

NSComparisonResult result = [number1 compare:number2];

return result == NSOrderedDescending; // 升序
// return result == NSOrderedAscending; // 降序
}];

NSArray排序方法

标签:

原文地址:http://www.cnblogs.com/paideblogs/p/5148629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!