zoukankan      html  css  js  c++  java
  • 针对NSString字符串的排序

    NSArray *ary = @[@"a3",@"a1",@"a2",@"a10",@"a24"];
    
    
    NSLog(@"%@",ary);
    
    
    NSArray *myary = [ary sortedArrayUsingComparator:^(NSString * obj1, NSString * obj2){
    
    
        return (NSComparisonResult)[obj1 compare:obj2 options:NSNumericSearch];
    
    
    }];
    
    
    NSLog(@"%@",myary);
    
    
    结果
    
    
     ( a3,a1, a2, a10, a24 )
    
    
     ( a1, a2,a3, a10, a24 )
    

     方法2

    NSArray *ary = @[@"a3",@"a1",@"a2",@"a24",@"a14"];
    
    
    NSLog(@"%@",ary);
    
    
    NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO];//yes升序排列,no,降序排列
    
    
    NSArray *myary = [ary sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sd1, nil]];//注意这里的ary进行排序后会生产一个新的数组指针,myary,不能在用ary,ary还是保持不变的。
    
    
    NSLog(@"%@",myary);
    
    
    //    (a3, a1, a2,a24,a14)
    
    
    //    (a3, a24, a2, a14, a1)
    

     方法3

    [ary sortedArrayUsingSelector:@selector(compare:)];//这个是一直默认升序
    
  • 相关阅读:
    CSAcademy Or Problem
    BZOJ 4516 [Sdoi2016] 生成魔咒
    SPOJ7258 SUBLEX
    SPOJ1812 LCS2
    SPOJ1811 LCS
    SPOJ8222 NSUBSTR
    洛谷3804 【模板】后缀自动机
    SPOJ287 NETADMIN
    SPOJ1693 COCONUTS
    BZOJ5329 SDOI2018 战略游戏
  • 原文地址:https://www.cnblogs.com/hezhuangzhuang/p/5116737.html
Copyright © 2011-2022 走看看