#import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) { 4 @autoreleasepool { 5 //数组内元素排序 6 NSArray *arr1=@[@"mon",@"tue",@"wed",@"thu",@"fri",@"sat",@"sun"]; 7 NSArray *arr2=[arr1 sortedArrayUsingSelector:@selector(compare:)]; 8 for(id str1 in arr2) { 9 NSLog(@"%@",str1); 10 } 11 //自定义排序——block(下面代码相当于冒泡排序) 12 NSComparator sort1=^(id obj1,id obj2){ //传入两个数组中的元素 13 NSComparisonResult result=[obj1 compare:obj2]; 14 return result; //根据自己定义返回一定的值 15 }; 16 NSArray *sort2=[arr1 sortedArrayUsingComparator:sort1]; //根据返回值排序 17 for(id str2 in sort2){ 18 NSLog(@"%@",str2); 19 } 20 } 21 return 0; 22 }