首页 > 代码库 > OC中的排序
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
OC中的排序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。