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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。