void sortArray4(){
//首先來(lái)3輛車(chē),分別是奧迪、勞斯萊斯、寶馬
Car *car1 = [Car initWithName:@"Audio"];
Car *car2 = [Car initWithName:@"Rolls-Royce"];
Car *car3 = [Car initWithName:@"BMW"];
//再來(lái)5個(gè)Person,每人送輛車(chē),分別為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];
//加入數(shù)組
NSArray *array = [NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil];
//構(gòu)建排序描述器
NSSortDescriptor *carNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"car.name" ascending:YES];
NSSortDescriptor *personNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSSortDescriptor *personAgeDesc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
//把排序描述器放進(jìn)數(shù)組里,放入的順序就是你想要排序的順序
//我這里是:首先按照年齡排序,然后是車(chē)的名字,最后是按照人的名字
NSArray *descriptorArray = [NSArray arrayWithObjects:personAgeDesc,carNameDesc,personNameDesc, nil];
NSArray *sortedArray = [array sortedArrayUsingDescriptors: descriptorArray];
NSLog(@"%@",sortedArray);
}