zoukankan      html  css  js  c++  java
  • OC中的排序

    void arraySort3() {  
        Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];  
        Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];  
        Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];  
        Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];  
        NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil];  
          
        // 利用block进行排序  
        NSArray *array2 = [array sortedArrayUsingComparator:  
         ^NSComparisonResult(Student *obj1, Student *obj2) {  
             // 先按照姓排序  
             NSComparisonResult result = [obj1.lastname compare:obj2.lastname];  
             // 如果有相同的姓,就比较名字  
             if (result == NSOrderedSame) {  
                 result = [obj1.firstname compare:obj2.firstname];  
             }  
               
             return result;  
        }];  
          
        NSLog(@"array2:%@", array2);  
    }  

    源:http://blog.csdn.net/daiyelang/article/details/18726947

  • 相关阅读:
    关于MySQL数据库中null的那些事
    Java集合之Collections 剖析
    字符串类
    C++标准库
    << 操作符
    操作符的重载
    类中的重载
    友元
    二阶构造模式
    静态成员函数
  • 原文地址:https://www.cnblogs.com/scaptain/p/4207070.html
Copyright © 2011-2022 走看看