- // 利用可变数组exchangeObjectAtIndex: NSUInteger i withObjectAtIndex:j交换变量
- NSMutableArray *marray = [NSMutableArray arrayWithArray:sortedArr];
-
- NSUInteger len = marray.count;
-
- for (int i =0; i<len; i++) {
- for (int j = i+1; j<len; j++) {
- if (marray[i]>marray[j]) {
- [marray exchangeObjectAtIndex:i withObjectAtIndex:j];
- }
- }
- }
- NSLog(@"%@",marray);
-
- // 利用block来进行排序
-
- NSArray *sortArray = [marray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
-
-
- if ([obj1 integerValue]<[obj2 integerValue]) {
- return NSOrderedAscending;
- }
-
-
- return NSOrderedSame;
- }];
-
- NSLog(@"%@",sortArray);
-
-
- // 利用系统函数正序排序
- NSArray *sort = [marray sortedArrayUsingSelector:@selector(compare:)];
- NSLog(@"正序排序\n%@",sort);
- // 倒序排序
- NSEnumerator *e = [sort reverseObjectEnumerator];
-
- NSLog(@"倒序排序\n%@",[e allObjects]);
复制代码
|