sortDescriptorWithKey:@"_age",以age做爲標準來作比較。 app
- Person *person1 = [[Person alloc] init];
- [person1 setName:@"ABCD"];
- [person1 setAge:24];
-
- Person *person2 = [[Person alloc] init];
- [person2 setName:@"ACBD"];
- [person2 setAge:22];
-
- Person *person3 = [[Person alloc] init];
- [person3 setName:@"ABDC"];
- [person3 setAge:33];
-
- Person *person4 = [[Person alloc] init];
- [person4 setName:@"ACDB"];
- [person4 setAge:22];
-
- NSMutableArray *array = [NSMutableArray arrayWithObjects:person1, person3, person4, person2, nil];
- [person1 release];
- [person2 release];
- [person3 release];
- [person4 release];
-
- NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"_age" ascending:YES];
- NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"_name" ascending:YES];
-
- NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];
-
- for(NSInteger i = 0; i < [tempArray count]; i++)
- {
- NSLog(@"%@--------%d\n", [[tempArray objectAtIndex:i] name], [[tempArray objectAtIndex:i] age]);
- }
-
- // [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]; atom
NSSortDescriptor *sortDescriptor2 = [NSSortDescriptorsortDescriptorWithKey:@"_name"ascending:YES]; spa
NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArrayarrayWithObjects:sortDescriptor1, sortDescriptor2, nil]]; .net