NSARRAY排序三种方法

(1)直接调用系统的方法排序int

NSMutableArray*array= [[NSMutableArrayalloc]init];
[arrayaddObject:[NSNumbernumberWithInt:20]];
[arrayaddObject:[NSNumbernumberWithInt:1]];
[arrayaddObject:[NSNumbernumberWithInt:4]];
NSArray*sortedArray= [arraysortedArrayUsingSelector:@selector(compare:)];
for(inti=0;i < [sortedArraycount];i++)
{
intx= [[sortedArrayobjectAtIndex:i]intValue];
NSLog(@"######%d\n",x);
}


(2)用descriptor方法

#import
 
@interfaceNode:NSObject {
intx;
inty;
intv;
}
 
@propertyintx;
@propertyinty;
@propertyintv;
-(id)init:(int)txy:(int)tyv:(int)tv;
@end
 
@implementationNode
 
@synthesizex;
@synthesizey;
@synthesizev;
 
-(id)init:(int)txy:(int)tyv:(int)tv{
x=tx;
y=ty;
v=tv;
returnself;
}
 
@end
 
intmain(intargc,char*argv[])
{
NSMutableArray *myMutableArray= [[NSMutableArrayalloc]init];
Node *n[2];
n[0]= [[Nodealloc]init:2y:1v:1];
n[1]= [[Nodealloc]init:4y:2v:2];
 
[myMutableArrayaddObject:n[0]];
[myMutableArrayaddObject:n[1]];
NSSortDescriptor*sortByA = [NSSortDescriptorsortDescriptorWithKey:@"x"ascending:NO];
[myMutableArraysortUsingDescriptors:[NSArrayarrayWithObject:sortByA]];
for(Node*tinmyMutableArray){
NSLog(@"x=== %d",t.x);
NSLog(@"y=== %d",t.y);
NSLog(@"v=== %d",t.v);
}
}


 

 

(3)自定义重写方法

 

#import
@implementation NSNumber(MySort)
-(NSComparisonResult)myCompare:(NSString *)other{
//这里可以作适当的修正后再比较
int result= ([selfintValue]>>1)- ([other intValue]>>1);
//这里可以控制排序的顺序和逆序
return result< 0 ?NSOrderedDescending :result >0 ?NSOrderedAscending :NSOrderedSame;
}
 
@end
int main(int argc, char *argv[])
{
NSMutableArray *array= [[NSMutableArrayalloc]init];
[arrayaddObject:[NSNumbernumberWithInt:20]];
[arrayaddObject:[NSNumbernumberWithInt:1]];
[arrayaddObject:[NSNumbernumberWithInt:4]];
NSArray *sortedArray= [arraysortedArrayUsingSelector:@selector(myCompare:)];
for(int i= 0;i < [sortedArraycount];i++)
{
int x= [[sortedArrayobjectAtIndex:i]intValue];
NSLog(@"######%d\n",x);
}
}


注:NSArraysortedArrayUsingDescriptors 不起作用?

原因:

 

erhaps taking a look at the two methods will be morebeneficial:

-[NSArray sortedArrayUsingDescriptors:]; 
-[NSMutableArray sortUsingDescriptors:];

The first one starts with an adjective and a noun. The adjectivedescribes the noun. The noun indicates that it is the return value.Since the noun is "array", it would make sense that the returnvalue of the method is an NSArray.Since the adjective is "sorted", we can therefore assume that thereturned array will be in some sort of order (the order beingspecified by the "using" the "descriptors").

The second one starts with a verb. Since a verb is an action word,we can deduce that this is actually a behavioral method that willmodify the receiver itself. Since the verb is "sort", we cantherefore assume that the receiver will be in some sort of orderafter the method has completed.

Other "method pairs" that follow this pattern:

  • -[NSStringstringByAppendingString:] and -[NSMutableStringappendString:]
  • -[NSArrayfilteredArrayUsingPredicate:] and -[NSMutableArrayfilterUsingPredicate:]
  • -[NSCharacterSetinvertedSet] and -[NSMutableCharacterSetinvert]
  • ...and many others

Since you're using the first method (sortedArrayUsingDescriptors:),you must retrievethe return value of the method; otherwise you're doing all thiswork for nothing. If you want to sort the array "in place" (ie,modify the receiver directly and not begiven a new NSArray),then personArray must bean NSMutableArray,and you must usethe sortUsingDescriptors: method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值