加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

NS类 - NSArray排序一(未完待续 。。。)

(2013-05-25 21:08:08)
标签:

nsarray排序

it

object-c数组排序

分类: Mac/IOS那些事
1.常用 正序排 利用数组的sortedArrayUsingComparator调用 NSComparator
  NSArray *_beforeSorted = [[NSArray alloc]initWithObjects:@"f",@"d",@"d",@"c",@"b",@"a", nil];
        NSArray *_sortedArray = [_beforeSorted sortedArrayUsingSelector:@selector(compare:)];
        NSLog(@"Before Sorted:%@",_beforeSorted);
        NSLog(@"After Sorted:%@",_sortedArray);
        [_beforeSorted release];
  output:
  Before Sorted:(
    f,
    d,
    d,
    c,
    b,
    a
)
 Sorted:(
    a,
    b,
    c,
    d,
    d,
    f
)
2.倒序排列(再1的基础上增加)
NSMutableArray *_reverseSorted = [[NSMutableArray alloc]init];
        for (id arrayItem in [_sortedArray reverseObjectEnumerator]) {
            [_reverseSorted addObject:arrayItem];
        }
NSLog(@"Reverse Sorted:%@",_reverseSorted);
[_reverseSorted release];
output:
 Reverse Sorted:(
    f,
    d,
    d,
    c,
    b,
    a
)

使用 sortUsingDescriptors 来实现按字典的键值来排列 利用sortUsingDescriptors调用NSSortDescriptor
        NSMutableArray *_beforeSortedArray = [[NSMutableArray alloc]init];
        [_beforeSortedArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"aaa",@"name",@"111",@"work", nil]];
        [_beforeSortedArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"ccc",@"name",@"333",@"work", nil]];
        [_beforeSortedArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"fff",@"name",@"222",@"work", nil]];
        [_beforeSortedArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"bbb",@"name",@"444",@"work", nil]];
        NSSortDescriptor *_sortByName = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
        NSSortDescriptor *_sortByWork = [[NSSortDescriptor alloc]initWithKey:@"work" ascending:YES];
        NSArray *_sortByNameArray = [[NSArray alloc]initWithObjects:&_sortByName count:1];
        NSArray *_sortByWorkArray = [[NSArray alloc]initWithObjects:&_sortByWork count:1];
        NSLog(@"befor Sorted:%@",_beforeSortedArray);
        [_beforeSortedArray sortUsingDescriptors:_sortByNameArray];
        NSLog(@"Sorted by Name:%@",_beforeSortedArray);
        [_beforeSortedArray sortUsingDescriptors:_sortByWorkArray];
        NSLog(@"Sorted By Work:%@",_beforeSortedArray);
output:
 befor Sorted:(
        {
        name = aaa;
        work = 111;
    },
        {
        name = ccc;
        work = 333;
    },
        {
        name = fff;
        work = 222;
    },
        {
        name = bbb;
        work = 444;
    }
)
 Sorted by Name:(
        {
        name = aaa;
        work = 111;
    },
        {
        name = bbb;
        work = 444;
    },
        {
        name = ccc;
        work = 333;
    },
        {
        name = fff;
        work = 222;
    }
)
 Sorted By Work:(
        {
        name = aaa;
        work = 111;
    },
        {
        name = fff;
        work = 222;
    },
        {
        name = ccc;
        work = 333;
    },
        {
        name = bbb;
        work = 444;
    }
src01 :http://blog.sina.com.cn/s/blog_9c3c519b0101725l.html
需要补充 sortedArrayUsingFunction 自定义函数的排序法 。。。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有