ios 数组排序

1.数组逆置(不排序,只是把数组元素逆置一下)

//ObjectEnumerator        正序  

//1.原始数组  
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];  
//2.倒序的数组  
NSArray* reversedArray = [[array reverseObjectEnumerator] allObjects];  

2.数组排序的几种方法

1>

使用NSComparator进行排序

NSMutableArray *tempArray = [NSMutableArray arrayWithArray:@[@"5",@"3",@"4",@"6",@"2",@"0",@"1",]];
    NSArray * aaArray = [tempArray sortedArrayUsingComparator:^NSComparisonResult(NSString * obj1, NSString * obj2) {
        //升序
        return [obj1 compare:obj2];
    }];
    
    NSArray * bbArray = [tempArray sortedArrayUsingComparator:^NSComparisonResult(NSString * obj1, NSString * obj2) {
        //降序
        return [obj2 compare:obj1];
    }];

[tempArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return obj1.xxx < obj2.xxx;
    }]

2>

使用NSDescriptor进行排序

ort descriptor不仅可以用来对数组进行排序,还能指定element在table view中的排序,以及Core Data中对fetch request返回的数据做排序处理。通过sort descriptor可以很方便的对数组进行多个key的排序。下面来看看如何对我们的数组做surname排序,然后在进行name排序:

  1. NSSortDescriptor *firstDescriptor = [[NSSortDescriptor alloc] initWithKey:@"surname" ascending:YES]; 
  2. NSSortDescriptor *secondDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 
  3. NSArray *sortDescriptors = [NSArray arrayWithObjects:firstDescriptor, secondDescriptor, nil]; 
  4. NSArray *sortedArray = [self.persons sortedArrayUsingDescriptors:sortDescriptors]; 

上面代码的排序结果如下所示:

  1. Andersen Jane 
  2. Clark Anne 
  3. Johnson Rose 
  4. Smith David 
  5. Smith John 
3>

使用selector进行排序

#pragma mark 数组排序1  
void arraySort1() {  
    NSArray *array = [NSArray arrayWithObjects:@"2", @"3", @"1", @"4", nil nil];  
      
    // 返回一个排好序的数组,原来数组的元素顺序不会改变  
    // 指定元素的比较方法:compare:  
    NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];  
    NSLog(@"array2:%@", array2);  
}  
  
#pragma mark 数组排序2  
void arraySort2() {  
    Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];  
    Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];  
    Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];  
    Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];  
    NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil];  
    // 指定排序的比较方法  
    NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];  
    NSLog(@"array2:%@", array2);  
}  
- (NSComparisonResult)compareStudent:(Student *)stu {  
    // 先按照姓排序  
    NSComparisonResult result = [self.lastname compare:stu.lastname];  
    // 如果有相同的姓,就比较名字  
    if (result == NSOrderedSame) {  
        result = [self.firstname compare:stu.firstname];  
    }  
    return result;  
}  
  
#pragma mark 数组排序3  
void arraySort3() {  
    Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];  
    Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];  
    Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];  
    Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];  
    NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil];  
      
    // 利用block进行排序  
    NSArray *array2 = [array sortedArrayUsingComparator:  
     ^NSComparisonResult(Student *obj1, Student *obj2) {  
         // 先按照姓排序  
         NSComparisonResult result = [obj1.lastname compare:obj2.lastname];  
         // 如果有相同的姓,就比较名字  
         if (result == NSOrderedSame) {  
             result = [obj1.firstname compare:obj2.firstname];  
         }  
           
         return result;  
    }];  
      
    NSLog(@"array2:%@", array2);  
}  
  
#pragma mark 数组排序4-高级排序  
void arraySort4() {  
    Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li" bookName:@"book1"];  
    Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang" bookName:@"book2"];  
    Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li" bookName:@"book2"];  
    Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao" bookName:@"book1"];  
    NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil];  
      
    // 1.先按照书名进行排序  
    // 这里的key写的是@property的名称  
    NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];  
    // 2.再按照姓进行排序  
    NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];  
    // 3.再按照名进行排序  
    NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];  
    // 按顺序添加排序描述器  
    NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil nil];  
      
    NSArray *array2 = [array sortedArrayUsingDescriptors:descs];  
      
    NSLog(@"array2:%@", array2);  
} 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值