(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);
}
}
注:NSArraysortedArrayUsingDescript
原因:
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:]
-
-[NSArrayfilteredArrayUsingPredic
ate:] and -[NSMutableArrayfilterUsingPredicate:]
-
-[NSCharacterSetinvertedSet]
and -[NSMutableCharacterSetinvert]
- ...and many others
Since you're using the first method (sortedArrayUsingDescript
),you NSArray
),then personArray
NSMutableArray
,and you sortUsingDescriptors: