zoukankan      html  css  js  c++  java
  • objectc NSArray排序小结

    ios的排序不知道是用强大来形容呢,还是要用复杂来形容,反正觉得不如php一个sort函数来的简洁,每次用排序都得去网上现查怎么实现,这不查的次数多了也便有了这个小小的总结。
    1.升序排列

      NSArray *_firstArray = [NSArray arrayWithObjects:@"ccccc",@"bbbbb",@"ddddd",@"aaaaa",nil];
      NSArray *_sortedArray= [_firstArray sortedArrayUsingSelector:@selector(compare:)];
      NSLog(@"未排序:%@",_firstArray);
      NSLog(@"排行后:%@",_sortedArray);
    2012-03-19 23:17:06.748 IOSSortArray[1761:f803] 未排序:(
        ccccc,
        bbbbb,
        ddddd,
        aaaaa
    )
    2012-03-19 23:17:06.749 IOSSortArray[1761:f803] 排行后:(
        aaaaa,
        bbbbb,
        ccccc,
        ddddd
    )

    2.降序排列

      NSMutableArray *_reSortArray= [[NSMutableArray alloc] init];
      for (id _obj in [_sortedArray reverseObjectEnumerator]) {
        [_reSortArray addObject:_obj];
      }
      NSLog(@"降序排列:%@",_reSortArray);
      [_reSortArray release];
    2012-03-19 23:27:20.789 IOSSortArray[2011:f803] 排行后:(
        aaaaa,
        bbbbb,
        ccccc,
        ddddd
    )
    2012-03-19 23:27:20.790 IOSSortArray[2011:f803] 降序排列:(
        ddddd,
        ccccc,
        bbbbb,
        aaaaa
    )

    3.数组里的字典排序(NSDirector排序)

      NSMutableArray *_mutiArray = [[NSMutableArray alloc] init];
      [_mutiArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"rainbird",@"name",@"ddabc",@"work", nil]];
      [_mutiArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"ainbird",@"name",@"ccabc",@"work", nil]];
      [_mutiArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"inbird",@"name",@"fdabc",@"work", nil]];
      [_mutiArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"bird",@"name",@"agbc",@"work", nil]];
      [_mutiArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"rd",@"name",@"abac",@"work", nil]];
      
      NSSortDescriptor *_sorter  = [[NSSortDescriptor alloc] initWithKey:@"name" 
                                                               ascending:YES];
      NSSortDescriptor *_sorter2 = [[NSSortDescriptor alloc] initWithKey:@"work" 
                                                               ascending:YES];
      NSLog(@"未排序:\n%@",_mutiArray);
      NSLog(@"根据name排序:\n%@",[_mutiArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:_sorter, nil]]);
      NSLog(@"根据work排序:\n%@",[_mutiArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:_sorter2, nil]]);
      
      [_mutiArray release];
      [_sorter release];
      [_sorter2 release];
    2012-03-19 23:34:37.489 IOSSortArray[2119:f803] 未排序:
    (
            {
            name = rainbird;
            work = ddabc;
        },
            {
            name = ainbird;
            work = ccabc;
        },
            {
            name = inbird;
            work = fdabc;
        },
            {
            name = bird;
            work = agbc;
        },
            {
            name = rd;
            work = abac;
        }
    )
    2012-03-19 23:34:37.490 IOSSortArray[2119:f803] 根据name排序:
    (
            {
            name = ainbird;
            work = ccabc;
        },
            {
            name = bird;
            work = agbc;
        },
            {
            name = inbird;
            work = fdabc;
        },
            {
            name = rainbird;
            work = ddabc;
        },
            {
            name = rd;
            work = abac;
        }
    )
    2012-03-19 23:34:37.491 IOSSortArray[2119:f803] 根据work排序:
    (
            {
            name = rd;
            work = abac;
        },
            {
            name = bird;
            work = agbc;
        },
            {
            name = ainbird;
            work = ccabc;
        },
            {
            name = rainbird;
            work = ddabc;
        },
            {
            name = inbird;
            work = fdabc;
        }
    )

    *******************************

    sortedArrayUsingSelector:@selector(caseInsensitiveCompare:) //不区分大小排序

    哈哈这么说来再扩展一下的话,还有更多的选择,比如数字排序,只根据第几位到第几位排序等

    - (NSComparisonResult)compare:(NSString *)string;
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; // locale arg used to be a dictionary pre-Leopard. We now accepts NSLocale. Assumes the current locale if non-nil and non-NSLocale.
    - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
    - (NSComparisonResult)localizedCompare:(NSString *)string;
    - (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string;

    /* localizedStandardCompare:, added in 10.6, should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate.  The exact behavior of this method may be tweaked in future releases, and will be different under different localizations, so clients should not depend on the exact sorting order of the strings.
    */
    - (NSComparisonResult)localizedStandardCompare:(NSString *)string NS_AVAILABLE(10_6, 4_0);
     
     
  • 相关阅读:
    [导入]遍历表单的代码,包括遍历所有,以及遍历特定tag,如input,select lcs
    [导入]JS更改onclick事件 lcs
    [导入]SQLite 字符串长度 函数 lcs
    [导入]休眠 睡眠 区别 lcs
    【转】OpenGL鼠标点击事件
    【转】键盘按键和键盘对应代码表
    【转】vs2010快捷键;sql server 2008快捷;IE9快捷键
    【转】vc6.0移植到VS2010遇到的问题,散分给大家,虽然分不多,各位帮忙
    【转】单缓冲与双缓冲的区别
    【转】光照、材质、纹理的关系
  • 原文地址:https://www.cnblogs.com/yingkong1987/p/2705508.html
Copyright © 2011-2022 走看看