码迷,mamicode.com
首页 > 编程语言 > 详细

数组的几种排序方式

时间:2017-08-03 22:17:12      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:方式   dem   span   sde   integer   产生   scom   for   contex   

    // 1.使用NSComparator排序
    {
        // 产生随机数
        NSMutableArray *unsortDataM = [NSMutableArray array];
        for (NSInteger index = 0; index < 20; index++) {
            int random = arc4random()%20;
            [unsortDataM addObject:@(random)];
        }
        NSLog(@"使用NSComparator排序前的数据:%@",unsortDataM);
        
        NSArray *sortedData = [unsortDataM sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSInteger value1 = (NSInteger)obj1;
            NSInteger value2 = (NSInteger)obj2;
            
            if (value1 < value2) {
                return NSOrderedAscending;
            }else {
                return NSOrderedDescending;
            }
        }];
        
        NSLog(@"使用NSComparator排序后的数据:%@",sortedData);

    }
    
    // 2.使用NSDescriptor排序
    {
        Person *person1 = [[Person alloc] init];
        person1.age = 39;
        person1.height = 178;
        
        Person *person2 = [[Person alloc] init];
        person2.age = 25;
        person2.height = 165;
        
        Person *person3 = [[Person alloc] init];
        person3.age = 55;
        person3.height = 170;
        
        Person *person4 = [[Person alloc] init];
        person4.age = 15;
        person4.height = 165;
        
        Person *person5 = [[Person alloc] init];
        person5.age = 38;
        person5.height = 177;
        
        NSMutableArray *unsortDataM = [NSMutableArray arrayWithObjects:person1,person2,person3,person4,person5, nil];
        NSLog(@"使用NSDescriptor排序前的数据:%@",unsortDataM);
        
        NSSortDescriptor *firstDescriptor  = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
        NSSortDescriptor *secondDescriptor = [[NSSortDescriptor alloc] initWithKey:@"height" ascending:YES];
        
        // 第一优先级先按照年龄排,年龄相同的按照第二优先级排序
        NSArray *sortArray = [NSArray arrayWithObjects:firstDescriptor,secondDescriptor,nil];
        NSArray  *sortedData = [unsortDataM sortedArrayUsingDescriptors:sortArray];
        NSLog(@"使用NSDescriptor排序后的数据");
        for (Person *person in sortedData) {
            NSLog(@"age:%d,height:%d",person.age,person.height);
        }
    }
    
    // 3.使用函数排序
    {
    
        // 产生随机数
        NSMutableArray *unsortDataM = [NSMutableArray array];
        for (NSInteger index = 0; index < 20; index++) {
            int random = arc4random()%20;
            [unsortDataM addObject:@(random)];
        }
        NSLog(@"使用函数排序前的数据:%@",unsortDataM);
        NSArray  *sortedData = [unsortDataM sortedArrayUsingFunction:customSort context:nil];
        NSLog(@"使用函数排序后的数据:%@",sortedData);
    }
#pragma mark - 排序函数

NSInteger customSort(id obj1, id obj2,void* context) {
    int value1 = (int)obj1;
    int value2 = (int)obj2;
    
    if (value1 < value2) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    
    if (value1 > value2) {
        return (NSComparisonResult)NSOrderedDescending;
    }
    
    return (NSComparisonResult)NSOrderedSame;
}

demo地址:https://github.com/HZChenCheng/SortArray

数组的几种排序方式

标签:方式   dem   span   sde   integer   产生   scom   for   contex   

原文地址:http://www.cnblogs.com/HZChenCheng/p/7281748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!