voidArraySort3 () {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]; //Sort by blockNsarray *array2 =[Array sortedarrayusingcomparator:^nscomparisonresult (Student *obj1, Student *obj2) { //Sort by last name firstNscomparisonresult result =[Obj1.lastname Compare:obj2.lastname]; //If you have the same last name, compare the names . if(Result = =nsorderedsame) {Result=[Obj1.firstname Compare:obj2.firstname]; } returnresult; }]; NSLog (@"array2:%@", array2); }
Source: http://blog.csdn.net/daiyelang/article/details/18726947
Sorting in OC