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

posted on 2015-01-06 21:57  SCaptain  阅读(288)  评论(0编辑  收藏  举报

导航