Objective-c NSArrray数据操作
2017-03-30 本文已影响0人
凯旋之歌
数据源:NSArray *array = @[@10.1,@10.5,@12,@9.2,@109.4,@39.3];
1.获得最大值
NSNumber *max = [array valueForKeyPath:@"@max.floatValue"];
//关键字 @max 通过指定数据类型得到对应的NSNumber数据
2.获得最小值
NSNumber *min = [array valueForKeyPath:@"@min.floatValue"];
3.获得数据和
NSNumber *sum = [array valueForKeyPath:@"@sum.floatValue"];
4.获得数据平均值
NSNumber *avg = [array valueForKeyPath:@"@avg.floatValue"];
5.返回数组中大于3的元素
NSPredicate *pre = [NSPredicate predicateWithFormat:@"self > 20"];
NSArray *preArray = [array filteredArrayUsingPredicate:pre];
6.从小到大排序
NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(compare:)];
7.从大到小排序
NSComparator comptr = ^(id obj1, id obj2){
return (-1)*[obj1 compare:obj2];
};
NSArray *revertSortedArray = [array sortedArrayWithOptions:NSSortStable usingComparator:comptr];