iOS 数组排序实例
iOS 数组排序
NSMutableArray
*p = [[NSMutableArray alloc]
initWithObjects:@"S1",@"S14",@"S3",@"S4",@"S12",@"S6",@"S7",@"S8",@"S9",@"S10",@"S11",@"S5",@"S13",@"S2",nil];
答案:(比比谁的方法简单,高效)
NSMutableArray *p = [[NSMutableArray alloc] initWithObjects:@"S1",@"S14",@"S3",@"S4",@"S12",@"S6",@"S7",@"S8",@"S9",@"S10",@"S11",@"S5",@"S13",@"S2",nil];
[p sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *a = (NSString *)obj1;
NSString *b = (NSString *)obj2;
int aNum = [[a substringFromIndex:1] intValue];
int bNum = [[b substringFromIndex:1] intValue];
if (aNum > bNum) {
return NSOrderedDescending;
}
else if (aNum < bNum){
return NSOrderedAscending;
}
else {
return NSOrderedSame;
}
}];
NSLog(@"p %@", p);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。