zoukankan      html  css  js  c++  java
  • 排序 遍历

    #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;

    }

  • 相关阅读:
    如何用密码保护共享文件?
    如何让光驱自动弹出和关闭?
    欢迎参加“诊断 Windows 7 启动及登录缓慢问题”在线技术会议
    谁偷走了我的系统资源?
    修改Windows验证的登陆框为页面
    使用Fiddler提高前端工作效率
    对非管理员隐藏Site Actions的菜单
    C#多线程参数传递
    转:管理网站集所使用的内容数据库
    转:十步骤让你轻松提升SharePoint性能
  • 原文地址:https://www.cnblogs.com/lcl15/p/4918327.html
Copyright © 2011-2022 走看看