NSArray 排序

发布时间:2017-1-17 19:06:01 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"NSArray 排序",主要涉及到NSArray 排序方面的内容,对于NSArray 排序感兴趣的同学可以参考一下。

转:NSArray 内容排序 //1. 数字型字符串 NSArray *originalArray = @[@"1",@"21",@"12",@"11",@"0"]; //block比较方法,数组中可以是NSInteger,NSString(需要转换) NSComparator finderSort = ^(id string1,id string2){ if ([string1 integerValue] > [string2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; }else if ([string1 integerValue] < [string2 integerValue]){ return (NSComparisonResult)NSOrderedAscending; } else return (NSComparisonResult)NSOrderedSame; }; //数组排序: NSArray *resultArray = [originalArray sortedArrayUsingComparator:finderSort]; NSLog(@"第一种排序结果:%@",resultArray); //2. 非数字型字符串 NSArray *charArray = @[@"string 1",@"String 21",@"string 12",@"String 11",@"String 02"]; NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch|NSNumericSearch| NSWidthInsensitiveSearch|NSForcedOrderingSearch; NSComparator sort = ^(NSString *obj1,NSString *obj2){ NSRange range = NSMakeRange(0,obj1.length); return [obj1 compare:obj2 options:comparisonOptions range:range]; }; NSArray *resultArray2 = [charArray sortedArrayUsingComparator:sort]; NSLog(@"字符串数组排序结果%@",resultArray2); // 3字典对象 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 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 == NSOrderedAscending; // 降序 return result == NSOrderedDescending; // 升序 }]; NSLog(@"OrderedDescending:%@", resultArray); //4.自定义对象 Person *person1 = [[Person alloc] init]; [person1 setName:@"ABCD"]; [person1 setAge:24]; Person *person2 = [[Person alloc] init]; [person2 setName:@"ACBD"]; [person2 setAge:22]; Person *person3 = [[Person alloc] init]; [person3 setName:@"ABDC"]; [person3 setAge:33]; Person *person4 = [[Person alloc] init]; [person4 setName:@"ACDB"]; [person4 setAge:22]; NSMutableArray *array = [NSMutableArray arrayWithObjects:person1, person3, person4, person2, nil]; NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]; //先按照age排序, NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; //如果age相同, 按照name排序,以此类推 NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil]]; for(NSInteger i = 0; i < [tempArray count]; i++){ NSLog(@"%@--------%d\n", [[tempArray objectAtIndex:i] name], [[tempArray objectAtIndex:i] age]); } #import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic, strong) NSString *name; @property (nonatomic, assign) NSInteger age; @end#import "Person.h" @implementation Person @synthesize name; @synthesize age; @end

上一篇:
下一篇:linux 命令行操作 记录

相关文章

关键词: NSArray 排序

相关评论