1.sortedArrayUsingSelector (sort nsdictionary by key value size)
Nsmutablearray *array = [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]; Nsarray *resultarray = [Array sortedarrayusingselector: @selector (compare:)];
Because Nsdictionary does not have a compare sort comparison method, we need to write a
-(Nscomparisonresult) Compare: (Nsdictionary *) otherdictionary{ nsdictionary *tempdictionary = (nsdictionary *) Self; NSNumber *number1 = [[Tempdictionary AllKeys] objectatindex:0]; NSNumber *number2 = [[Otherdictionary AllKeys] objectatindex:0]; Nscomparisonresult result = [Number1 compare:number2]; return result = = Nsordereddescending; Ascending// return result = = nsorderedascending; Descending
2.sortedArrayUsingComparator
Nsmutablearray *array = [Nsmutablearray arraywithobjects: [Nsdictionary Dictionarywithobje ctsandkeys:@ "Obj0", [NSNumber numberwithint:0], nil], [Nsdictionary dictionarywithobjectsandke ys:@ "Obj5", [NSNumber Numberwithint:5], nil], [nsdictionary dictionarywithobjectsandkeys:@ "OBJ 2 ", [NSNumber Numberwithint:2], nil], [nsdictionary dictionarywithobjectsandkeys:@" Obj3 ", [NSN Umber Numberwithint:3], nil], [nsdictionary dictionarywithobjectsandkeys:@ "Obj1", [NSNumber nu Mberwithint:1], nil], [nsdictionary dictionarywithobjectsandkeys:@ "Obj4", [NSNumber Numberwith Int:4], nil], nil]; Nsarray *resultarray = [Array sortedarrayusingselector: @selector (compare:)]; Nsarray *resultarray = [array sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {nsnumber * Number1 = [[Obj1 AllKeys] ObjectaTINDEX:0]; NSNumber *number2 = [[Obj2 AllKeys] objectatindex:0]; Nscomparisonresult result = [Number1 compare:number2]; return result = = Nsordereddescending; Ascending//return result = = Nsorderedascending; Descending}];3.sortedArrayUsingDescriptors & Sortusingdescriptors The former with the return value, is the Nsarray method, the sorted array is the return value of the array, the latter without the return value, Is the Nsmutablearray method, which is to sort the current array by itself and then arrange the object according to the properties of an object
. h@interface person:nsobject{ nsstring *_name; Nsinteger _age;} @property (nonatomic, retain) nsstring *name; @property (nonatomic, assign) Nsinteger age ; @end. m@ Implementation Person@synthesize name = _name; @synthesize Age = _age;-(void) dealloc{ [_name release]; [Super Dealloc];} @end
Implementation of the sorting method
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]; [Person1 release]; [Person2 release]; [Person3 release]; Here is similar to Kvo's method of reading properties, read the object properties directly from the string, note not to write wrong nssortdescriptor *sortdescriptor = [Nssortdescriptor sortdescriptorwithkey:@ " _age "Ascending:yes"; This array holds the sorted object Nsarray *temparray = [array Sortedarrayusingdescriptors:[nsarray arraywithobject:sortdescriptor]]; for (Nsinteger i = 0; i < [Temparray count]; i++) {NSLog (@ "%@--------%d\n", [[Temparray objectatindex:i] Name], [[Temparray objectatindex:i] age]); }//below is a variable array method//[Array Sortusingdescriptors:[nsarray arraywithobject:sortdescriptor]];//// for (Nsinteger i = 0; i < [array count]; i++)//{//NSLog (@ "%@--------%d\n", [[Array objectatindex:i] name] , [[[Array objectatindex:i] age]);
Nssortdescriptor *sortdescriptor1 = [Nssortdescriptorsortdescriptorwithkey:@ "_age"Ascending :YES];
Nssortdescriptor *sortdescriptor2 = [Nssortdescriptorsortdescriptorwithkey:@ "_name" Ascending:YES];
Nsarray *temparray = [array sortedarrayusingdescriptors:[nsarrayArraywithobjects:sortdescriptor1, SortDescriptor2, Nil]];
The first element in the Nsarray here indicates that the order is first sorted in ascending or descending order of this element, for duplicates, then by the second element, and so on.
iOS Development--arrays