博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转】NSArray排序方法
阅读量:5908 次
发布时间:2019-06-19

本文共 6382 字,大约阅读时间需要 21 分钟。

原文网址:http://www.cnblogs.com/xiaobaizhu/archive/2013/06/05/3119983.html

从网上查的,非常方便的排序api,功能也很强大

1.sortedArrayUsingSelector

(按Key值大小对NSDictionary排序)
NSMutableArray *array = [NSMutableArray arrayWithObjects:                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber numberWithInt:0], nil],                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber numberWithInt:5], nil],                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj2", [NSNumber numberWithInt:2], nil],                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj3", [NSNumber numberWithInt:3], nil],                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj1", [NSNumber numberWithInt:1], nil],                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj4", [NSNumber numberWithInt:4], nil], nil];    NSArray *resultArray = [array sortedArrayUsingSelector:@selector(compare:)];

因为NSDictionary没有compare的排序比较方法,所以需要我们自己写一个

- (NSComparisonResult)compare: (NSDictionary *)otherDictionary{    NSDictionary *tempDictionary = (NSDictionary *)self;        NSNumber *number1 = [[tempDictionary allKeys] objectAtIndex:0];    NSNumber *number2 = [[otherDictionary allKeys] objectAtIndex:0];        NSComparisonResult result = [number1 compare:number2];        return result == NSOrderedDescending; // 升序//    return result == NSOrderedAscending;  // 降序}

2.sortedArrayUsingComparator

NSMutableArray *array = [NSMutableArray arrayWithObjects:                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber numberWithInt:0], nil],                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber numberWithInt:5], nil],                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj2", [NSNumber numberWithInt:2], nil],                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj3", [NSNumber numberWithInt:3], nil],                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj1", [NSNumber numberWithInt:1], nil],                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj4", [NSNumber numberWithInt:4], nil], nil];    //    NSArray *resultArray = [array sortedArrayUsingSelector:@selector(compare:)];        NSArray *resultArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {                NSNumber *number1 = [[obj1 allKeys] objectAtIndex:0];        NSNumber *number2 = [[obj2 allKeys] objectAtIndex:0];                NSComparisonResult result = [number1 compare:number2];                return result == NSOrderedDescending; // 升序//        return result == NSOrderedAscending;  // 降序    }];

3.sortedArrayUsingDescriptors & sortUsingDescriptors

前者带返回值,是NSArray的方法,排好序的数组是返回值中的数组;
后者不带返回值,是NSMutableArray的方法,是对当前数组自己排序
接下来根据一个对象的属性,排列这个对象
.h@interface Person : NSObject{    NSString  *_name;        NSInteger  _age;}@property (nonatomic, retain) NSString  *name;@property (nonatomic, assign) NSInteger  age;@end.m@implementation Person@synthesize name = _name;@synthesize age  = _age;- (void)dealloc{    [_name release];        [super dealloc];}@end

排序方法的实现

Person *person1 = [[Person alloc] init];    [person1 setName:@"ABC"];    [person1 setAge:24];        Person *person2 = [[Person alloc] init];    [person2 setName:@"ACB"];    [person2 setAge:22];        Person *person3 = [[Person alloc] init];    [person3 setName:@"ABD"];    [person3 setAge:33];        NSMutableArray *array = [NSMutableArray arrayWithObjects:person1, person2, person3, nil];    [person1 release];    [person2 release];    [person3 release];    //这里类似KVO的读取属性的方法,直接从字符串读取对象属性,注意不要写错    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"_age" ascending:YES];    //这个数组保存的是排序好的对象    NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];        for(NSInteger i = 0; i < [tempArray count]; i++)    {        NSLog(@"%@--------%d\n", [[tempArray objectAtIndex:i] name], [[tempArray objectAtIndex:i] age]);    }//下面是可变数组的方法   //    [array sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];//    //    for(NSInteger i = 0; i < [array count]; i++)//    {//        NSLog(@"%@--------%d\n", [[array objectAtIndex:i] name], [[array objectAtIndex:i] age]);//    }

   NSSortDescriptor *sortDescriptor1 = [NSSortDescriptorsortDescriptorWithKey:@"_age"ascending:YES];

   NSSortDescriptor *sortDescriptor2 = [NSSortDescriptorsortDescriptorWithKey:@"_name"ascending:YES];

   NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArrayarrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];

   这里的NSArray中的第一元素表示首先按照这个元素的升序或者降序进行排序,对于有重复项的,再按照第二元素进行排序,依次进行类推

 

原文网址:http://blog.csdn.net/wenluma/article/details/8705272

  1. NSMutableArray *array =  [[NSMutableArray alloc] initWithObjects:@"1",@"3",@"5",@"40" nil];</span></p>NSArray *sorteArray = [array sortedArrayUsingComparator:^(id obj1, id obj2){  
  2.     if ([obj1 integerValue] > [obj2 integerValue]) {  
  3.         return (NSComparisonResult)NSOrderedDescending;  
  4.     }  
  5.       
  6.     if ([obj1 integerValue] < [obj2 integerValue]) {  
  7.         return (NSComparisonResult)NSOrderedAscending;  
  8.     }  
  9.       
  10.     return (NSComparisonResult)NSOrderedSame;  
  11. }];  
  12.   
  13. NSLog(@"%@",sorteArray);            //从小到大  
  14.   
  15.   
  16. NSArray *array2 = [array sortedArrayUsingComparator:^(id obj1, id obj2){  
  17.     if ([obj1 integerValue] > [obj2 integerValue]) {  
  18.         return (NSComparisonResult)NSOrderedAscending;  
  19.     }  
  20.       
  21.     if ([obj1 integerValue] < [obj2 integerValue]) {  
  22.         return (NSComparisonResult)NSOrderedDescending;  
  23.     }  
  24.       
  25.     return (NSComparisonResult)NSOrderedSame;  
  26. }];  
  27.   
  28. NSLog(@"%@",array2);  

以上包含了有从小到大的排序,也包含有大到小的排序

如果是针对字符串的排序,有更好的方法,

 

[cpp]   
 
  1. NSArray *ary = @[@"a3",@"a1",@"a2",@"a10",@"a24"];  
  2. NSLog(@"%@",ary);  
  3. NSArray *myary = [ary sortedArrayUsingComparator:^(NSString * obj1, NSString * obj2){  
  4.     return (NSComparisonResult)[obj1 compare:obj2 options:NSNumericSearch];  
  5. }];  
  6. NSLog(@"%@",myary);  
  7. 结果  
  8.  ( a3,a1, a2, a10, a24 )  
  9.  ( a1, a2,a3, a10, a24 )  

 

 

[cpp]   
 
  1. NSArray *ary = @[@"a3",@"a1",@"a2",@"a24",@"a14"];  
  2. NSLog(@"%@",ary);  
  3. NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO];//yes升序排列,no,降序排列  
  4. NSArray *myary = [ary sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sd1, nil]];//注意这里的ary进行排序后会生产一个新的数组指针,myary,不能在用ary,ary还是保持不变的。  
  5. NSLog(@"%@",myary);  
  6. //    (a3, a1, a2,a24,a14)  
  7. //    (a3, a24, a2, a14, a1)  
[cpp]   
 
  1. [ary sortedArrayUsingSelector:@selector(compare:)];//这个是一直默认升序 
你可能感兴趣的文章
Unity3d的Sprite Packer用法介绍
查看>>
Caused by: java.sql.SQLException: ORA-24816: 在实际的 LONG 或 LOB 列之后提供了扩展的非 LONG 绑定数据...
查看>>
bash快捷建-光标移到行首、行尾等
查看>>
两行代码解决端口冲突
查看>>
set(集合)
查看>>
jquery学习之add()
查看>>
K-Anonymous Sequence
查看>>
ASP过滤HTML的通用函数
查看>>
Dojo 1.7 Release Notes翻译完成,包含若干重大更新
查看>>
关于org.slf4j.LoggerFactory
查看>>
如何解决WebBrowser.DocumentCompleted事件的多次调用
查看>>
JAVA_内部类
查看>>
UVa 10566 - Crossed Ladders 【二分】
查看>>
CF79D Password
查看>>
jxl 导入excel
查看>>
poj 2983 Is the Information Reliable? (差分约束)
查看>>
POJ 2479 Maximum sum【最大连续和2】
查看>>
手机体验系列之三
查看>>
在ubuntu中用apt-get安装LEMP栈(linux+nginx+mysql+php)
查看>>
javaWeb项目-文件下载的消息头和编码问题
查看>>