- ios 排序汇总
-
几种简单有效的数组排序方法
//第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象NSComparator cmptr = ^(id obj1, id obj2){
if ([obj1 integerValue] > [obj2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; } if ([obj1 integerValue] < [obj2 integerValue]) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame; }; NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil]; //排序前 NSMutableString *outputBefore = [[NSMutableString alloc] init]; for(NSString *str in sortArray){ [outputBefore appendFormat:@"]; } NSLog(@"排序前:%@",outputBefore); [outputBefore release]; //第一种排序 NSArray *array = [sortArray sortedArrayUsingComparator:cmptr]; NSMutableString *outputAfter = [[NSMutableString alloc] init]; for(NSString *str in array){ [outputAfter appendFormat:@"]; } NSLog(@"排序后:%@",outputAfter); [outputAfter release];NSComparator cmptr = ^(id obj1, id obj2){
if ([obj1 integerValue] > [obj2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; } if ([obj1 integerValue] < [obj2 integerValue]) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame;};NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil]; //排序前 NSMutableString *outputBefore = [[NSMutableString alloc] init]; for(NSString *str in sortArray){ [outputBefore appendFormat:@"]; } NSLog(@"排序前:%@",outputBefore); [outputBefore release]; //第一种排序 NSArray *array = [sortArray sortedArrayUsingComparator:cmptr]; NSMutableString *outputAfter = [[NSMutableString alloc] init]; for(NSString *str in array){ [outputAfter appendFormat:@"]; } NSLog(@"排序后:%@",outputAfter); [outputAfter release];第二种 排序方法 利用sortedArrayUsingFunction 调用 对应方法customSort,这个方法中的obj1和obj2分别是指数组中的对象。
[plain]NSInteger customSort(id obj1, id obj2,void* context){ if ([obj1 integerValue] > [obj2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; } if ([obj1 integerValue] < [obj2 integerValue]) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame; } NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil]; //排序前 NSMutableString *outputBefore = [[NSMutableString alloc] init]; for(NSString *str in sortArray){ [outputBefore appendFormat:@"]; } NSLog(@"排序前:%@",outputBefore); [outputBefore release]; NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil]; NSMutableString *outputAfter = [[NSMutableString alloc] init]; for(NSString *str in array){ [outputAfter appendFormat:@"]; } NSLog(@"排序后:%@",outputAfter); [outputAfter release];NSInteger customSort(id obj1, id obj2,void* context){
if ([obj1 integerValue] > [obj2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; } if ([obj1 integerValue] < [obj2 integerValue]) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame;} NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil]; //排序前 NSMutableString *outputBefore = [[NSMutableString alloc] init]; for(NSString *str in sortArray){ [outputBefore appendFormat:@"]; } NSLog(@"排序前:%@",outputBefore); [outputBefore release]; NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil]; NSMutableString *outputAfter = [[NSMutableString alloc] init]; for(NSString *str in array){ [outputAfter appendFormat:@"]; } NSLog(@"排序后:%@",outputAfter); [outputAfter release];第三种 利用sortUsingDescriptors调用NSSortDescriptor
[plain]
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price" ascending:NO];//其中,price为数组中的对象的属性,这个针对数组中存放对象比较更简洁方便 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1]; [_totalInfoArray sortUsingDescriptors:sortDescriptors]; [_airListView refreshTable:_totalInfoArray]; [sortDescriptor release]; [sortDescriptors release];NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price" ascending:NO];//其中,price为数组中的对象的属性,这个针对数组中存放对象比较更简洁方便
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1]; [_totalInfoArray sortUsingDescriptors:sortDescriptors]; [_airListView refreshTable:_totalInfoArray]; [sortDescriptor release]; [sortDescriptors release]; 20120619更新 排序操作,NSArray提供了很多种方法,按照所利用的排序参数,可以简单分为descriptor,selector,function,和block排序。数组排序的时候有sortedArrayUsingComparator方法,这里的参数为NSComparator,这个类其实是Block所定义的typedef NSComparisonResult (^NSComparator)(id obj1,id obj2);所以我们直接用NSComparator myComparator=^(id obj1,id obj2){};Block其实可以看作JS里面的(){}();方法以及.Net中的匿名方法,即不需要声明就直接使用的方法,主要用处包括回调、事件处理和排序。声明和实现可以分开,也可以写在一起:[plain] view plaincopyprint?NSInteger (^blockExample)(int,NSString*)=^(int i,NSString *s) { NSLog(@"%d,%@",i,s); }; blockExample(42,@"hello");NSInteger (^blockExample)(int,NSString*)=^(int i,NSString *s)
{ NSLog(@"%d,%@",i,s);};blockExample(42,@"hello");对于NSArray排序或者其他需要Block作为参数的函数来说,可以写成下面两种形式://声明和使用 [plain]NSComparison ( ^ageComparator )( id, id ) = ^( id o1, id o2 ) { // 比较函数的实现 }; NSArray* objs_sortedByComparator = [objs sortedArrayUsingComparator:ageComparator]; // 不声明,彻底匿名使用 NSArray* objs_sortedByComparator = [objs sortedArrayUsingComparator:^( id o1, id o2 ) { //... }];NSComparison ( ^ageComparator )( id, id ) = ^( id o1, id o2 ) {
// 比较函数的实现}; NSArray* objs_sortedByComparator = [objs sortedArrayUsingComparator:ageComparator];// 不声明,彻底匿名使用 NSArray* objs_sortedByComparator = [objs sortedArrayUsingComparator:^( id o1, id o2 ) { //... }];例子:
[plain]
NSArray *array = [NSArray arrayWithObjects:@"71", @"16", @"51", @"104", @"13",@"2",@"1", nil]; array = [array sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2) { return (arc4random() % 3) - 1; }]; // NSLog(@"array:%@", array); NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) { if ([obj1 integerValue] > [obj2 integerValue]) { return (NSComparisonResult)NSOrderedAscending; } if ([obj1 integerValue] < [obj2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; } return (NSComparisonResult)NSOrderedSame; }]; sortedArray 是排好序的时间对象数组 // 按时间排序 NSArray *sortedArray = [srcArrary sortedArrayUsingComparator:^(id obj1, id obj2) { NSComparisonResult result = [obj1 compare:obj2]; switch(result) { case NSOrderedAscending: return NSOrderedDescending; case NSOrderedDescending: return NSOrderedAscending; case NSOrderedSame: return NSOrderedSame; default: return NSOrderedSame; } // 时间从近到远(远近相对当前时间而言) }];NSArray *array = [NSArray arrayWithObjects:@"71", @"16", @"51", @"104", @"13",@"2",@"1", nil];
array = [array sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2) { return (arc4random() % 3) - 1; }]; // NSLog(@"array:%@", array); NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) { if ([obj1 integerValue] > [obj2 integerValue]) { return (NSComparisonResult)NSOrderedAscending; } if ([obj1 integerValue] < [obj2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; } return (NSComparisonResult)NSOrderedSame; }];//随机排序
[plain]NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", nil]; array = [array sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2) { return (arc4random() % 3) - 1; }]; NSLog(@"array:%@", array);NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
array = [array sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2) { return (arc4random() % 3) - 1; }]; NSLog(@"array:%@", array);