void sortArray4(){
//首先来3辆车,分别是奥迪、劳斯莱斯、宝马
Car *car1 = [Car initWithName:@
"Audio"
];
Car *car2 = [Car initWithName:@
"Rolls-Royce"
];
Car *car3 = [Car initWithName:@
"BMW"
];
//再来5个Person,每人送辆车,分别为car2、car1、car1、car3、car2
Person *p1 = [Person personWithAge:23 withName:@
"zhangsan"
withCar:car2];
Person *p2 = [Person personWithAge:21 withName:@
"zhangsan"
withCar:car1];
Person *p3 = [Person personWithAge:24 withName:@
"lisi"
withCar:car1];
Person *p4 = [Person personWithAge:23 withName:@
"wangwu"
withCar:car3];
Person *p5 = [Person personWithAge:23 withName:@
"wangwu"
withCar:car2];
//加入数组
NSArray *array = [NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil];
//构建排序描述器
NSSortDescriptor *carNameDesc = [NSSortDescriptor sortDescriptorWithKey:@
"car.name"
ascending:YES];
NSSortDescriptor *personNameDesc = [NSSortDescriptor sortDescriptorWithKey:@
"name"
ascending:YES];
NSSortDescriptor *personAgeDesc = [NSSortDescriptor sortDescriptorWithKey:@
"age"
ascending:YES];
//把排序描述器放进数组里,放入的顺序就是你想要排序的顺序
//我这里是:首先按照年龄排序,然后是车的名字,最后是按照人的名字
NSArray *descriptorArray = [NSArray arrayWithObjects:personAgeDesc,carNameDesc,personNameDesc, nil];
NSArray *sortedArray = [array sortedArrayUsingDescriptors: descriptorArray];
NSLog(@
"%@"
,sortedArray);
}