zoukankan      html  css  js  c++  java
  • 数组排序

    iphone开发数组排序(数组中包括字典)  

     
    1。普通数组排序:
         NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"0",@"8",@"6",@"1",nil];
         NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
    if ([obj1 intValue] > [obj2 intValue]){
    return NSOrderedDescending;
    }
    if ([obj1 intValue] < [obj2 intValue]){
    return NSOrderedAscending;
    }
    return NSOrderedSame;
    }];
          NSLog(@"排序后的数组:%@",sortedArray);
     
    2.数组中有字典的排序:
        NSMutableArray *myMutableArr = [[[NSMutableArray alloc] init] autorelease];
        NSDictionary *dicOne = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"price",@"2",@"number", nil];
        NSDictionary *dicTWo = [NSDictionary dictionaryWithObjectsAndKeys:@"6",@"price",@"5",@"number", nil];
        NSDictionary *dicThree = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"price",@"1",@"number", nil];
        NSDictionary *dicFour = [NSDictionary dictionaryWithObjectsAndKeys:@"4",@"price",@"3",@"number", nil];
        [myMutableArr addObject:dicOne];
        [myMutableArr addObject:dicTWo];
        [myMutableArr addObject:dicThree];
        [myMutableArr addObject:dicFour];
        
        NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES]];
        [myMutableArr sortUsingDescriptors:sortDescriptors];
        NSLog(@"排序后的数组%@",myMutableArr);
     
    经验证,上面的方式相当方便,尤其是数组中有字典的情况。
  • 相关阅读:
    DVD X Player 5.5 PRO
    Freefloat FTP Server 1.0漏洞分析
    基于约束的SQL攻击
    Commons-Collections漏洞
    Code-Audit-Challenges-php-2
    GSM Sniffer环境--c118+osmocombb
    XXE (XML External Entity Injection) :XML外部实体注入
    hyperledger fabric学习(1)
    zero to one (4)
    zero to one (3)
  • 原文地址:https://www.cnblogs.com/yunis/p/3897693.html
Copyright © 2011-2022 走看看