首页 > 代码库 > iosOC/C不可变数组排序
iosOC/C不可变数组排序
//1.回顾C数组排序
int a[6] = {1,4,3,5,6,2};
//选择
for (int i =0; i<6-1; i++) {
for (int j = i+1; j<6;j++) {
if(a[i]>a[j]){
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
for(int i=0;i<6;i++){
printf("a[%d] = %d\n",i,a[i]);
}
//NSArray 排序
NSArray * array = @[@"1",@"4",@"5",@"7",@"2",@"9"];
//当前放返回值是数组,这个排序是系统默认提供,遵从从小大 具有局限性 不方便扩展 不会经常使用
[array sortedArrayUsingSelector:@selector(compare:)];
// [array sortedArrayWithOptions:<#(NSSortOptions)#> usingComparator:<#^NSComparisonResult(id obj1, id obj2)cmptr#>]
// [array sortedArrayUsingFunction:<#(NSInteger (*)(__strong id, __strong id, void *))#> context:<#(void *)#>]
NSMutableArray * arr2 = [[NSMutableArray alloc]initWithArray:array];
for (int i = 0; i<[arr2 count]-1; i++) {
for (int j = i+1; j<[arr2 count]; j++) {
if(([arr2[i] intValue])>([arr2[j] intValue])){
NSString * str = arr2[i];
arr2[i] = arr2[j];
arr2[j] = str;
}
}
}
NSLog(@"%@",arr2);
iosOC/C不可变数组排序