#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//字符串排序
NSArray *arr=@[@"234",@"123",@"345"];
NSArray *s=[arr sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",s);
//描述器排序
NSSortDescriptor *s1=[NSSortDescriptorsortDescriptorWithKey:@"对象的属性"ascending:NO];//yes是升序,no降序
NSArray *arr1=@[s1];//arr1中可以放多个排序要求,在前的要先排(前主后次)
NSArray *arr2=[arr1 sortedArrayUsingDescriptors:arr1];
//自定义对象排序
-(NSComparisonResult) compareWithage:(Student *) stu
{
NSComparisonResult result =[[NSNumber numberWithint:self.age] Component:[NSNumber numberWithint:stu.age]];//self在前为升序,在后为降序
return result ;
}
//block(代码块)排序
NSArray *sortArr2 = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];}];//根据比较结果,如果结果是1,则交换
}
return 0;
}
//排序数组中对象的属性
NSArray *arr=@[s1,s2,s3,s4];
NSArray *sortArr = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSComparisonResult result = [[obj1 name] compare: [obj2 name]];
return result;
}];
NSLog(@"%@",sortArr);
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *arr=@[@"123",@"abc",@"456",@"def"];
for (int i=0; i<arr.count; i++) {
NSLog(@"%@",arr[i]);
}
//快速遍历
for(id obj in arr) {
NSLog(@"--%@",obj);
}
//使用枚举器进行遍历
NSEnumerator *e = [arr objectEnumerator];//获取数组所有元素的枚举器
NSString *obj;
//[e nestObject];第一次遍历,nextObject指向数组的首元素,当取值结束以后,再指向下一个元素,指导取万最后一个元素,此时,nextObject指向null,值为0,结束遍历
while (obj = [e nextObject]) {
NSLog(@"-->%@",obj);
}
}
return 0;
}