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);