黑马程序员技术交流社区

标题: NSArray的一个方法 [打印本页]

作者: 桑托尔    时间: 2014-5-5 14:44
标题: NSArray的一个方法
本帖最后由 桑托尔 于 2014-5-5 15:36 编辑

请问这个方法的详细用法 谢谢!
-(NSArray *)sortedArrayUsingDescriptors: (NSArray *)sortDescriptors
作者: 吉大人    时间: 2014-5-5 15:08
大哥你得把问题贴出来,描述不清无法回答
作者: ios_fce    时间: 2014-5-5 17:31
  1. void arraySort() {
  2.     Student *stu1 = [[Student alloc] initWithName:@"lisi" andAge:18 andScore:87];
  3.     Student *stu2 = [[Student alloc] initWithName:@"wangwu" andAge:19 andScore:60];
  4.     Student *stu3 = [[Student alloc] initWithName:@"zhangsan" andAge:28 andScore:89.5];
  5.     Student *stu4 = [[Student alloc] initWithName:@"zhaoliu" andAge:38 andScore:99];
  6.     Student *stu5 = [[Student alloc] initWithName:@"liMing" andAge:17 andScore:79];
  7.    
  8.     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4,stu5, nil];
  9.     NSLog(@"未排序前--------------------------------------\n");
  10.     [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  11.         NSLog(@"%@",obj);
  12.     }];
  13.    
  14.     // 1.先按照成绩进行排序
  15.     // 这里的key写的是get的名称
  16.     NSSortDescriptor *scoreDesc = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES];
  17.     // 2.再按照年龄进行排序
  18.     NSSortDescriptor *ageDesc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
  19.     // 3.再按照姓名进行排序
  20.     NSSortDescriptor *nameDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
  21.     // 按顺序添加排序描述器
  22.     NSArray *descs = [NSArray arrayWithObjects:scoreDesc, ageDesc, nameDesc, nil];
  23.    
  24.     NSArray *array2 = [array sortedArrayUsingDescriptors:descs];// 按升序排列
  25.     NSLog(@"排序后--------------------------------------");
  26.     [array2 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  27.         NSLog(@"%@",obj);
  28.     }];
  29.    
  30.    
  31. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2