NSMutableArray 排序 --- 三种方法
2012-02-23 22:39
295 查看
(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<Cocoa/Cocoa.h>
@interfaceNode: NSObject {
intx;
inty;
intv;
}
@property intx;
@property inty;
@property intv;
- (id)init:(int)tx
y:(int)ty v:(int)tv;
@end
@implementationNode
@synthesizex;
@synthesizey;
@synthesizev;
- (id)init:(int)tx
y:(int)ty v:(int)tv
{
x= tx;
y= ty;
v= tv;
return self;
}
@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)自定义重写方法
/*
Abstract:Constants returned by comparison functions, indicating whether a value is equal to, less than, or greater than
another value.
Declaration:enumCFComparisonResult{
kCFCompareLessThan=
-1,
kCFCompareEqualTo=
0,
kCFCompareGreaterThan=
1
};
*/
#import<Cocoa/Cocoa.h>
@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);
}
}
http://hi.baidu.com/popln/blog/item/4b16c3ae918df0d97cd92ae3.html
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<Cocoa/Cocoa.h>
@interfaceNode: NSObject {
intx;
inty;
intv;
}
@property intx;
@property inty;
@property intv;
- (id)init:(int)tx
y:(int)ty v:(int)tv;
@end
@implementationNode
@synthesizex;
@synthesizey;
@synthesizev;
- (id)init:(int)tx
y:(int)ty v:(int)tv
{
x= tx;
y= ty;
v= tv;
return self;
}
@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)自定义重写方法
/*
Abstract:Constants returned by comparison functions, indicating whether a value is equal to, less than, or greater than
another value.
Declaration:enumCFComparisonResult{
kCFCompareLessThan=
-1,
kCFCompareEqualTo=
0,
kCFCompareGreaterThan=
1
};
*/
#import<Cocoa/Cocoa.h>
@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);
}
}
http://hi.baidu.com/popln/blog/item/4b16c3ae918df0d97cd92ae3.html
相关文章推荐
- NSMutableArray中各种自定义排序方法
- NSArray,NSMutableArray的三种排序
- OC学习笔记15--Foundation之NSArray与NSMutableArray常用方法
- NSArray和NSMutableArray常用方法总结
- NSMutableArray 排序
- NSMutableString 和 NSMutableArray常用方法
- NSMutableArray和NSArray删除相同元素并只保留一个的方法
- NSMutableArray的创建方法
- ios数组排序NSMut 4000 ableArray *temp = [NSMutableArray arrayWithObjects:@"5",@"1",@"4",@"2",nil];
- NSArray和NSMutableArray的常用方法
- 常见的NSArray和NSMutableArray方法
- NSMutableArray复杂对象排序
- iOS: NSMutableArray的方法removeObjectAtIndex:
- NSArray和NSMutableArray常用方法及注意事项
- NSMutableArray创建使用方法和心得
- 十一、常用的NSArray和NSMutableArray方法
- NSArray & NSMutableArray 常用方法
- 防止忘记初始化NSMutableArray的方法
- 常见的NSArray和NSMutableArray方法
- NSMutableArray调用addObjectsFromArray方法崩溃问题