数组内元素排序及数组排序
#define NSLog(FORMAT, ...) fprintf(stderr,"%s\n",[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *arr1=@[@"mon",@"tue",@"wed",@"thu",@"fri",@"sat",@"sun"];
//1、数组内元素排序
NSArray *sortarr=[arr1 sortedArrayUsingSelector:@selector(compare:)];
for(id str in sortarr)
{
NSLog(@"%@",str);
}
NSLog(@"\n");
//2、数组排序-block
NSComparator block=^(id obj1,id obj2)
{
//NSComparisonResult result=[obj2 compare:obj1];
//return result=NSOrderedAscending;//升序排序按ASCILL代码排的
return [obj2 compare:obj1];
};
NSArray *sortArray=[arr1 sortedArrayUsingComparator:block];
for(id str in sortArray)
{
NSLog(@"%@",str);
}
NSLog(@"\n");
//插入一个元素,按原来拍好的顺序规则插入到数组中
NSMutableArray *b=[NSMutableArray arrayWithArray:sortArray];
[b addObject:@"sum"];
NSArray *sortArrays=[b sortedArrayUsingComparator:block];
for(id str in sortArrays)
{
NSLog(@"%@",str);
}
}
return 0;
}