#Use the ArraySortedarrayusingcomparatorCall nscomparator
Nscomparator is actually a block that returns nscomparisonresult.
Typedef nscomparisonresult (^ nscomparator) (ID obj1, Id obj2); where obj1 and obj2 are actually elements in nsarray
resultArray = [arrayDic <span style="color:#009900;">sortedArrayUsingComparator:</span>^NSComparisonResult(id obj1, id obj2) { NSNumber * number1 = [[obj1 allKeys] objectAtIndex:0]; NSNumber * number2 = [[obj2 allKeys] objectAtIndex:0]; NSComparisonResult result = [number1 compare:number2]; return result == NSOrderedAscending; }];
#Use the sortedarrayusingfunction of the array to call the corresponding method customsort
Nsinteger <span style = "color: # ff9900;"> sortbyid </span> (ID obj1, Id obj2, void * context) {nsstring * str1 = (nsstring *) obj1; // ibj1 and obj2 come from your array. In fact, I personally think Apple has implemented a bubble sort for you to use nsstring * str2 = (nsstring *) obj2; if (str1.length <str2.length) {<span style = "white-space: pre"> </span> return nsordereddescending;} else if (str1.length = str2.length) {<span style = "white-space: pre"> </span> return nsorderedsame;} <span style = "white-space: pre "> </span> return nsorderedascending ;}
NSArray *sortedArray =[arr sortedArrayUsingFunction:sortByID context:nil];
# Using arraySortedarrayusingselector calls the corresponding sel Method
Note that the selector method is for array elements. If the data element does not have the compare method, you can add the corresponding method by extending the class of array elements.
NSMutableArray *arrayDic = [NSMutableArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber numberWithInt:0], nil], [NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber numberWithInt:5], nil], [NSDictionary dictionaryWithObjectsAndKeys:@"Obj2", [NSNumber numberWithInt:2], nil], [NSDictionary dictionaryWithObjectsAndKeys:@"Obj3", [NSNumber numberWithInt:3], nil], [NSDictionary dictionaryWithObjectsAndKeys:@"Obj1", [NSNumber numberWithInt:1], nil], [NSDictionary dictionaryWithObjectsAndKeys:@"Obj4", [NSNumber numberWithInt:4], nil], nil];#ifdef sortedArrayUsingSelector resultArray = [arrayDic sortedArrayUsingSelector:@selector(compare:)];
Because the elements in the array correspond to dictionaries, the dictionary classes are extended.
@ Implementation Dictionary (extend)-(optional) compare: (nsdictionary *) otherdictionary {nsnumber * number2 = [[otherdictionary allkeys] objectatindex: 0]; nsdictionary * tempdictionary = (nsdictionary *) self; nsnumber * number1 = [[tempdictionary allkeys] objectatindex: 0]; nscomparisonresult result = [number1 compare: number2]; // return result = nsordereddescending; // return result = nsorderedascending in ascending order; // descending order}
# Using arraySortusingdescriptors call nssortdescriptor
Nssortdescriptor can be simply understood as a comparative description of an attribute of a specified object.
/*** It is convenient to sort by a certain attribute in the model object. * sortdescriptor1 array is sorted in ascending order by name * sortdescriptor2 array is sorted in descending order by age * two types of sortdescriptor add an array, sort by name first, and sort by age with the same name */person * person1 = [[person alloc] init]; [person1 setname: @ "ABC"]; [person1 setage: 24]; person * person2 = [[person alloc] init]; [person2 setname: @ "ACB"]; [person2 setage: 22]; person * person3 = [[person alloc] init]; [person3 setname: @ "Abd"]; [person3 setage: 33]; nsmutablearray * array = [nsmutablearray arraywithobjects: person1, person2, person3, nil]; nssortdescriptor * sortdescriptor1 = [nssortdescriptor <span style = "color: # ff0000;"> sortdescriptorwithkey </span>: @ "_ name" ascending: yes]; nssortdescriptor * sortdescriptor2 = [nssortdescriptor <span style = "color: # ff0000;"> sortdescriptorwithkey </span>: @ "_ age" ascending: No]; resultarray = [array sortedarrayusingdescriptors: [nsarray arraywithobjects: sortdescriptor1, sortdescriptor2, nil]; for (nsinteger I = 0; I <[resultarray count]; I +) {nslog (@ "% @ -------- % d \ n", [[resultarray objectatindex: I] Name], [[resultarray objectatindex: I] age]);}